sy 2.0.16 → 2.0.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb410690b2e92f7288bccda84c4ca3cc9a723d74
4
- data.tar.gz: 152de97d675381866eb049aec7a81cfdcc5e111c
3
+ metadata.gz: f0cdb5e7240e7affdf2b08f083324fa67ad9e8a7
4
+ data.tar.gz: bfe8fdb86bfe504b2b49e0104e5f8ee60d37d549
5
5
  SHA512:
6
- metadata.gz: d970e1667a67997b2d0b0e1b1d917301e60087c2c6ae064d4e7cf20fa570bdbc5c228273daa664a8fca3d98e4387f6ef0599b5bd01ac031234fbaa84c61e2feb
7
- data.tar.gz: 5d6f1abdcd1027416f4a6891971de69f612a9683cbef9ba31e59fd84b1d2d6b8294b894c5be2dc1ddb43b5ca92bc6519be011af4c354d2cb25baa85035ab0168
6
+ metadata.gz: 4d6c7473e4bc14e0567ab14026e292356fc2e7b83d729c8580aaa53208f6881f7eaf666eee790e4f92406748eaaee8bbdd00a652da68940aa0a726695df14ce5
7
+ data.tar.gz: 7039bc22bf98e02d491073becf7ffee9be34e91307c832c46f2c88a1e757aa127eba07210a05d3766fa1bbc40fa70ea1f7fe6b463990f8b63b2453b8153f4805
data/lib/sy.rb CHANGED
@@ -22,6 +22,7 @@ require_relative 'sy/magnitude'
22
22
  require_relative 'sy/absolute_magnitude'
23
23
  require_relative 'sy/signed_magnitude'
24
24
  require_relative 'sy/unit'
25
+ require_relative 'sy/matrix'
25
26
 
26
27
  # The most prominent feature of SY is, that it extends the Numeric class
27
28
  # with methods corresponding to units and their abbreviations.
data/lib/sy/magnitude.rb CHANGED
@@ -1,4 +1,5 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: utf-8
2
+
2
3
  # This module defines common assets of a magnitude – be it absolute (number of
3
4
  # unit objects), or relative (magnitude difference).
4
5
  #
@@ -27,6 +28,12 @@ module SY::Magnitude
27
28
  def zero( of: nil )
28
29
  absolute of: of, amount: 0
29
30
  end
31
+
32
+ # Magnitude 1 of a given quantity.
33
+ #
34
+ def one( of: nil )
35
+ absolute of: of, amount: 1
36
+ end
30
37
  end
31
38
 
32
39
  # Magnitudes respond to unit methods.
@@ -124,6 +131,8 @@ module SY::Magnitude
124
131
  magnitude amount * m2
125
132
  # when SY::ZERO then
126
133
  # return magnitude 0
134
+ when Matrix then
135
+ m2.map { |e| self * e }
127
136
  else
128
137
  ( quantity * m2.quantity ).magnitude( amount * m2.amount )
129
138
  end
@@ -137,6 +146,8 @@ module SY::Magnitude
137
146
  magnitude amount / m2
138
147
  # when SY::ZERO then
139
148
  # raise ZeroDivisionError, "Attempt to divide #{self} by #{SY::ZERO}."
149
+ when Matrix then
150
+ amount / m2 * quantity.magnitude( 1 )
140
151
  else
141
152
  ( quantity / m2.quantity ).magnitude( amount / m2.amount )
142
153
  end
@@ -174,6 +185,8 @@ module SY::Magnitude
174
185
  def coerce m2
175
186
  if m2.is_a? Numeric then
176
187
  return SY::Amount.relative.magnitude( m2 ), self
188
+ elsif m2.is_a? Matrix then
189
+ return m2 * SY::UNIT, self
177
190
  elsif quantity.coerces? m2.quantity then
178
191
  return m2.( quantity ), self
179
192
  else
data/lib/sy/matrix.rb CHANGED
@@ -49,18 +49,36 @@ class Matrix
49
49
  when SY::Magnitude # newly added - multiplication by a magnitude
50
50
  # I am not happy with this explicit switch on SY::Magnitude type here.
51
51
  # Perhaps coerce should handle this?
52
- rows = Array.new row_size do |i|
53
- Array.new column_size do |j|
54
- self[i, j] * arg
55
- end
56
- end
57
- return self.class[ *rows ]
52
+ return map { |e| e * arg }
58
53
  else
59
54
  compat_1, compat_2 = arg.coerce self
60
55
  return compat_1 * compat_2
61
56
  end
62
57
  end
63
58
 
59
+ #
60
+ # Matrix division (multiplication by the inverse).
61
+ # Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]]
62
+ # => -7 1
63
+ # -3 -6
64
+ #
65
+ def /(other)
66
+ case other
67
+ when Numeric
68
+ rows = @rows.collect {|row|
69
+ row.collect {|e| e / other }
70
+ }
71
+ return new_matrix rows, column_count
72
+ when Matrix
73
+ return self * other.inverse
74
+ when SY::Magnitude # newly added - multiplication by a magnitude
75
+ return self * ( 1 / other )
76
+ else
77
+ return apply_through_coercion(other, __method__)
78
+ end
79
+ end
80
+
81
+
64
82
  # Creates a matrix of prescribed dimensions filled with wildcard zeros.
65
83
  #
66
84
  def Matrix.wildcard_zero r_count, c_count=r_count
data/lib/sy/quantity.rb CHANGED
@@ -421,6 +421,10 @@ class SY::Quantity
421
421
  new amount: 0, of: qnt
422
422
  end
423
423
 
424
+ define_method :one do # Constructor of unitary magnitudes
425
+ new amount: 1, of: qnt
426
+ end
427
+
424
428
  define_method :to_s do # Customized #to_s. It must be a proc,
425
429
  qnt_ɴ_λ.call % "Magnitude" # since the quantity owning @Magnitude
426
430
  end # might not be named yet as of now.
data/lib/sy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module SY
2
- VERSION = "2.0.16"
2
+ VERSION = "2.0.17"
3
3
  DEBUG = false # debug mode switch - sometimes there are lines like
4
4
  # puts "something" if SY::DEBUG
5
5
  end
data/test/sy_test.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/ruby
2
- # -*- coding: utf-8 -*-
2
+ # encoding: utf-8
3
3
 
4
4
  # **************************************************************************
5
5
  # THIS IS SPEC-STYLE TEST FILE FOR SY PHYSICAL UNITS LIBRARY
@@ -7,7 +7,6 @@
7
7
 
8
8
  # The following will load Ruby spec-style library
9
9
  require 'mathn'
10
- require 'minitest/spec'
11
10
  require 'minitest/autorun'
12
11
 
13
12
  # The following will load SY library
@@ -386,6 +385,8 @@ describe SY::Quantity, SY::Magnitude do
386
385
  assert_equal Matrix[[2.m, 3.m], [4.m, 5.m]],
387
386
  Matrix[[1.m, 2.m], [3.m, 4.m]] + Matrix[[1.m, 1.m], [1.m, 1.m]]
388
387
  assert_equal Matrix[[5.µM]], Matrix[[1.µM]] + Matrix[[2.µM.s⁻¹]] * Matrix[[2.s]]
388
+ assert_equal Matrix[[1.s]], Matrix[[1]] * 1.s
389
+ assert_equal Matrix[[1.s]], 1.s * Matrix[[1]]
389
390
  XOXO = SY::Unit.of SY::Volume, amount: 1.l
390
391
  assert_equal 1.l.( SY::Volume ), 1.xoxo.( SY::Volume )
391
392
  assert_equal SY::TRIPLE_POINT_OF_WATER, 0.°C.( SY::Temperature )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sy
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.16
4
+ version: 2.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2013-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport