Permalink
Browse files

Finish inversion

  • Loading branch information...
1 parent df2c8bc commit d5faa4f5af30d490e04881290c4f627508a15569 @ryanstout committed Dec 29, 2012
Showing with 39 additions and 26 deletions.
  1. +11 −17 README.md
  2. +13 −0 lib/stair_car/pmatrix/transforms.rb
  3. +3 −0 lib/stair_car/shared/errors.rb
  4. +0 −9 spec/pmatrix/pmatrix_spec.rb
  5. +12 −0 spec/pmatrix/transforms_spec.rb
View
@@ -134,6 +134,10 @@ Math is simple, just use the standard +,-,/ * does matrix multiplication and **
16 25 36
49 64 81
+### Transpose and inverse
+
+Transpose can be done with either matrix.transpose or ~matrix
+
### Stats
All stats operations can operate on the whole matrix, or just the columns or the rows. Just pass in a dimension to specify cols or rows (rows = 0, cols = 1)
@@ -196,6 +200,13 @@ You can also iterate or map on non-zero elements, which makes mapping sparse mat
0 0 0 0 20
0 0 0 0 0
+## Planned Features
+
+- JBLAS support
+- MRI support (perhaps with NMatrix)
+- More statistical features
+
+
## Installation
Add this line to your application's Gemfile:
@@ -210,20 +221,3 @@ Or install it yourself as:
$ gem install stair_car
-## Usage
-
-TODO: Write usage instructions here
-
-## Contributing
-
-1. Fork it
-2. Create your feature branch (`git checkout -b my-new-feature`)
-3. Commit your changes (`git commit -am 'Add some feature'`)
-4. Push to the branch (`git push origin my-new-feature`)
-5. Create new Pull Request
-
-
-
-TODO:
-- Use nil in .view_selection
-- sparse? fails on views - should have a view? also
@@ -8,6 +8,19 @@ def ~
return transpose
end
+ def inv
+ algebra = Java::cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra.new
+ begin
+ return PMatrix.new(algebra.inverse(@data))
+ rescue Java::JavaLang::IllegalArgumentException => e
+ if e.message == 'Matrix is singular.' || e.message == 'A is singular.'
+ raise InverseMatrixIsSignular, e.message
+ else
+ raise
+ end
+ end
+ end
+
def map(&block)
dup.map!(&block)
end
@@ -1,4 +1,7 @@
module StairCar
class MatrixDimensionsError < RuntimeError
end
+
+ class InverseMatrixIsSignular < RuntimeError
+ end
end
@@ -28,21 +28,12 @@
matrix[0,0].should == 5
end
- # it "should allow set and get with a single index on vectors" do
- # matrix = StairCar::PMatrix.spzeros(3,5)
- #
- # matrix[1,nil][2] == 4
- # matrix[1,2].should == 4
- # end
-
it 'should assign a matrix to a subview' do
matrix = StairCar::PMatrix.asc(3,3)
s = matrix[0,nil]
matrix[1,nil] = s
end
- it 'should invert'
-
it "should set via arrays" do
matrix = StairCar::PMatrix.zeros(3,4)
matrix[0,nil] = [1,2,3,4]
@@ -8,6 +8,18 @@
(~~matrix).should == matrix
end
+ it 'should invert' do
+ matrix = StairCar::PMatrix.new([[4,3],[3,2]])
+ matrix.inv.should == StairCar::PMatrix.new([[-2, 3], [3, -4]])
+ end
+
+ it 'should raise an error when it fails to invert' do
+ matrix = StairCar::PMatrix.asc(3,3)
+ lambda {
+ matrix.inv
+ }.should raise_error(StairCar::InverseMatrixIsSignular)
+ end
+
it "should map and map_non_zero" do
matrix = StairCar::PMatrix.zeros(2,3)
matrix[0,0] = 5

0 comments on commit d5faa4f

Please sign in to comment.