visual_array 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd21265c03d6ce6703f568a354c3138f64d497c72193f73562f873ccdfa49cf1
4
- data.tar.gz: 5665bf958cc7e1d0ea7f7d7e0d36ad79b561c69719c64d496bec2712c79b819d
3
+ metadata.gz: 7e1bdf3b43202bdd6d9e812b44f315fb8d3a9090bb777bf02406fe6e01343009
4
+ data.tar.gz: 29ea1764fddfa2be60f6af5405961a19a0051f60f74cf4cff218b13c2a860a5d
5
5
  SHA512:
6
- metadata.gz: 1a76ecda7711c46fb1bcdc837313ac5844b4d77c250f2b79a9631e5c5fc9d680c38fe15da23fb1410b30c61d7eabf83b76ee20bd860859df209744a6e0c786ee
7
- data.tar.gz: 19981713790f9e9f4c6fef2e3926b10d64183c514f60621a89bb98a58f7361ed6110ee75f9e47274ca52a1bcb07515a94e60f28170e15a531363c74ff8260ba5
6
+ metadata.gz: c7c2cda17b11facb770881b50bfc3085fae3e38184021e3e5b591c35d0f46fe2a37f67cf511923583d660733fe2fcdef4cb75d3e6bb8e18f4eafc2f9ebb02268
7
+ data.tar.gz: 48258c2a963cc0f8536217343f436ea3ebff30b1a08871d546306749b43fc286355e639aeee7cc21478cfe59b1c21492fd3607490d5b9044e4c41ee4def3aca9
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
- # VisualArray
1
+ ## RubyGems
2
+
3
+ The gem is available here [VisualArrayGem](https://rubygems.org/gems/visual_array)
2
4
 
5
+ # VisualArray
3
6
  ## Description
4
7
 
5
8
  **VisualArray** is a Ruby gem that displays arrays in a structured, visually appealing tabular format. It supports **1D, 2D, and multi-dimensional arrays** with options for customizing text **color**.
@@ -83,6 +86,8 @@ VisualArray.display(
83
86
  color: 'blue'
84
87
  )
85
88
  ```
89
+ ## Visual Representation
90
+ <img width="952" alt="image" src="https://github.com/user-attachments/assets/d9f143af-4dd5-4908-abc0-58385ec4e873" />
86
91
 
87
92
  ## Development
88
93
 
@@ -109,7 +114,7 @@ To release a new version:
109
114
  bundle exec rake release
110
115
  ```
111
116
 
112
- This will create a git tag, push commits, and publish the gem to [RubyGems](https://rubygems.org).
117
+ This will create a git tag, push commits, and publish the gem to [RubyGems](https://rubygems.org/gems/visual_array).
113
118
 
114
119
  ## Contributing
115
120
 
@@ -118,4 +123,3 @@ Bug reports and pull requests are welcome at [GitHub](https://github.com/chandu8
118
123
  ## License
119
124
 
120
125
  This project is available under the [MIT License](LICENSE.txt).
121
-
data/lib/operation.rb ADDED
@@ -0,0 +1,87 @@
1
+ module Operation
2
+ def matrix_multiply(matrix1, matrix2)
3
+ if matrix1.is_a?(Array) && matrix1.all? { |e| !e.is_a?(Array) } &&
4
+ matrix2.is_a?(Array) && matrix2.all? { |e| !e.is_a?(Array) }
5
+
6
+ if matrix1.length != matrix2.length
7
+ raise ArgumentError, "Vectors must have the same length for multiplication"
8
+ end
9
+
10
+ steps = matrix1.zip(matrix2).map { |a, b| "#{a} * #{b}" }
11
+ sum = matrix1.zip(matrix2).map { |a, b| a * b }.sum
12
+
13
+ return ["#{steps.join(' + ')} = #{sum}"]
14
+ end
15
+
16
+ if matrix1[0].length != matrix2.length
17
+ raise ArgumentError, "Matrices cannot be multiplied: incompatible dimensions"
18
+ end
19
+
20
+ rows1 = matrix1.length
21
+ cols1 = matrix1[0].length
22
+ cols2 = matrix2[0].length
23
+
24
+ result_display = Array.new(rows1) { Array.new(cols2, "") }
25
+
26
+ (0...rows1).each do |i|
27
+ (0...cols2).each do |j|
28
+ calc_steps = []
29
+ sum = 0
30
+
31
+ (0...cols1).each do |k|
32
+ calc_steps << "#{matrix1[i][k]} * #{matrix2[k][j]}"
33
+ sum += matrix1[i][k] * matrix2[k][j]
34
+ end
35
+
36
+ result_display[i][j] = "#{calc_steps.join(' + ')} = #{sum}"
37
+ end
38
+ end
39
+
40
+ result_display
41
+ end
42
+
43
+ def matrix_add(matrix1, matrix2)
44
+ matrix_elementwise_operation(matrix1, matrix2, :+)
45
+ end
46
+
47
+ def matrix_subtract(matrix1, matrix2)
48
+ matrix_elementwise_operation(matrix1, matrix2, :-)
49
+ end
50
+
51
+ def matrix_divide(matrix1, matrix2)
52
+ matrix_elementwise_operation(matrix1, matrix2, :/)
53
+ end
54
+
55
+ private
56
+
57
+ def matrix_elementwise_operation(matrix1, matrix2, operation)
58
+ if matrix1.is_a?(Array) && matrix1.all? { |e| !e.is_a?(Array) } &&
59
+ matrix2.is_a?(Array) && matrix2.all? { |e| !e.is_a?(Array) }
60
+
61
+ if matrix1.length != matrix2.length
62
+ raise ArgumentError, "Vectors must have the same length for #{operation}"
63
+ end
64
+
65
+ return matrix1.zip(matrix2).map { |a, b| "#{a} #{operation} #{b} = #{a.send(operation, b)}" }
66
+ end
67
+
68
+ if matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length
69
+ raise ArgumentError, "Matrices must have the same dimensions for #{operation}"
70
+ end
71
+
72
+ rows = matrix1.length
73
+ cols = matrix1[0].length
74
+
75
+ result_display = Array.new(rows) { Array.new(cols, "") }
76
+
77
+ (0...rows).each do |i|
78
+ (0...cols).each do |j|
79
+ calc_steps = "#{matrix1[i][j]} #{operation} #{matrix2[i][j]}"
80
+ result = matrix1[i][j].send(operation, matrix2[i][j])
81
+ result_display[i][j] = "#{calc_steps} = #{result}"
82
+ end
83
+ end
84
+
85
+ result_display
86
+ end
87
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VisualArray
4
- VERSION = "0.1.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/visual_array.rb CHANGED
@@ -1,13 +1,18 @@
1
1
  require 'terminal-table'
2
2
  require 'rainbow'
3
+ require_relative 'operation'
3
4
 
4
5
  module VisualArray
5
6
  class Error < StandardError; end
6
7
 
7
8
  class << self
8
- def display(array, color: "green")
9
+ include Operation
10
+ DEFAULT_COLOR = "green".freeze
11
+
12
+ def display(array, color: DEFAULT_COLOR)
9
13
  raise Error, "Input must be an array" unless array.is_a?(Array)
10
14
 
15
+ color ||= DEFAULT_COLOR
11
16
  dimensions = array_dimensions(array)
12
17
 
13
18
  case dimensions
@@ -20,6 +25,31 @@ module VisualArray
20
25
  end
21
26
  end
22
27
 
28
+ def multiply_and_display(matrix1, matrix2, color: DEFAULT_COLOR)
29
+ result_display = matrix_multiply(matrix1, matrix2)
30
+
31
+ puts Rainbow("\nMatrix Multiplication Result:\n").magenta.bright
32
+ display(result_display, color: color)
33
+ end
34
+
35
+ def add_and_display(matrix1, matrix2, color: DEFAULT_COLOR)
36
+ result_display = matrix_add(matrix1, matrix2)
37
+ puts Rainbow("\nMatrix Addition Result:\n").blue.bright
38
+ display(result_display, color: color)
39
+ end
40
+
41
+ def subtract_and_display(matrix1, matrix2, color: DEFAULT_COLOR)
42
+ result_display = matrix_subtract(matrix1, matrix2)
43
+ puts Rainbow("\nMatrix Subtraction Result:\n").red.bright
44
+ display(result_display, color: color)
45
+ end
46
+
47
+ def divide_and_display(matrix1, matrix2, color: DEFAULT_COLOR)
48
+ result_display = matrix_divide(matrix1, matrix2)
49
+ puts Rainbow("\nMatrix Division Result:\n").yellow.bright
50
+ display(result_display, color: color)
51
+ end
52
+
23
53
  private
24
54
 
25
55
  def array_dimensions(array)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visual_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chandanchoubey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-01 00:00:00.000000000 Z
11
+ date: 2025-03-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: VisualArray provides an easy way to represent and display multi-dimensional
14
14
  arrays in a visually appealing tabular format, making data analysis and debugging
@@ -21,6 +21,7 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - README.md
23
23
  - Rakefile
24
+ - lib/operation.rb
24
25
  - lib/visual_array.rb
25
26
  - lib/visual_array/version.rb
26
27
  - sig/visual_array.rbs