5123
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5084
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
TwoDimensionalMatrix variableSubclass:#SquareMatrix
|
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Collections-MultiDimensional'
|
|
12 |
!
|
|
13 |
|
|
14 |
|
5123
|
15 |
!SquareMatrix class methodsFor:'instance creation'!
|
|
16 |
|
|
17 |
withAll:elements
|
|
18 |
|numElements nRows|
|
|
19 |
|
|
20 |
numElements := elements size.
|
|
21 |
nRows := numElements integerSqrt.
|
|
22 |
self assert:(nRows * nRows == numElements) description:'number of elements is not square'.
|
|
23 |
^ (self new:numElements)
|
|
24 |
replaceFrom:1 with:elements;
|
|
25 |
setDimensions:{nRows . nRows }
|
|
26 |
|
|
27 |
"
|
|
28 |
Matrix3_3 withAll:#(11 12 13 21 22 23 31 32 33)
|
|
29 |
"
|
|
30 |
! !
|
|
31 |
|
5084
|
32 |
!SquareMatrix methodsFor:'matrix operations'!
|
|
33 |
|
|
34 |
determinant
|
|
35 |
"Answer the determinant of this matrix.
|
|
36 |
|
|
37 |
Note that if your only interest in the determinant is to decide whether or not the matrix is singular,
|
|
38 |
you're better off using #isSingular which is not subject to numeric overflow."
|
|
39 |
|
|
40 |
^ self determinantByLuDecomposition
|
|
41 |
|
|
42 |
"
|
|
43 |
(Matrix identity:3) determinantByCofactors
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
determinantByCofactors
|
|
48 |
"Answer the determinant of this matrix."
|
|
49 |
|
|
50 |
dimensions = #( 1 1 ) ifTrue: [ ^ self _at: 1 at: 1 ].
|
|
51 |
|
|
52 |
"General case"
|
|
53 |
|
|
54 |
^(1 to: self columns)
|
|
55 |
inject: 0
|
|
56 |
into: [ :tot :c | tot + ((self _at: 1 at: c) * (self cofactor: 1@c)) ]
|
|
57 |
!
|
|
58 |
|
|
59 |
determinantByLuDecomposition
|
|
60 |
"Answer the determinant of a matrix."
|
|
61 |
|
|
62 |
| lu prod |
|
|
63 |
|
|
64 |
lu := self luForm.
|
|
65 |
prod := self luForm rowInterchangeSign.
|
|
66 |
|
|
67 |
1 to: lu rows do: [ :i |
|
|
68 |
(lu at: i at: i) <= lu comparisonTolerance ifTrue: [ ^0 ].
|
|
69 |
prod := prod * (lu at: i at: i) asFraction ].
|
|
70 |
|
|
71 |
^prod
|
|
72 |
! !
|
|
73 |
|
|
74 |
!SquareMatrix methodsFor:'queries'!
|
|
75 |
|
|
76 |
isSquare
|
|
77 |
^ true
|
|
78 |
! !
|
|
79 |
|
|
80 |
!SquareMatrix class methodsFor:'documentation'!
|
|
81 |
|
|
82 |
version_CVS
|
|
83 |
^ '$Header$'
|
|
84 |
! !
|
|
85 |
|