Skip to content

Commit 861fbeb

Browse files
Update README.md
1 parent 3980394 commit 861fbeb

1 file changed

Lines changed: 51 additions & 22 deletions

File tree

README.md

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The `LANumeric` protocol denotes the type of numbers on which *LANumerics* opera
5555
* `Complex<Float>`
5656
* `Complex<Double>`
5757

58-
Most functionality of *LANumerics* is generic in `LANumeric`, e.g. solving a system of linear equations or computing the singular value decomposition of a matrix.
58+
Most functionality of *LANumerics* is generic in `LANumeric`, e.g. constructing matrices and computing with them, solving a system of linear equations, or computing the singular value decomposition of a matrix.
5959

6060
## Constructing Matrices
6161

@@ -107,63 +107,92 @@ It is also legal to create matrices with zero columns and/or rows, like `Matrix(
107107

108108
## SIMD Support
109109

110-
Swift supports `simd` vector and matrix operations. *LANumerics* plays nice with `simd` by providing conversion functions to and from `simd` vectors and matrices. For example,
110+
Swift supports `simd` vector and matrix operations. *LANumerics* plays nice with `simd` by providing conversion functions to and from `simd` vectors and matrices. For example, starting from
111111
```swift
112112
import simd
113113
import LANumerics
114114

115115
let m = Matrix(rows: [[1, 2, 3], [4, 5, 6]])
116116
print("m: \(m)")
117-
let s = m.simd3x2
118-
print("------------")
119-
print("as simd: \(s)")
120-
print("------------")
121-
print(Matrix(s) == m)
122117
```
123-
results in the output
118+
with output
124119
```
125120
m: 2x3-matrix:
126121
⎛1.0 2.0 3.0⎞
127122
⎝4.0 5.0 6.0⎠
128-
------------
129-
as simd: simd_double3x2(columns: (SIMD2<Double>(1.0, 4.0), SIMD2<Double>(2.0, 5.0), SIMD2<Double>(3.0, 6.0)))
130-
------------
131-
true
123+
```
124+
we can convert `m` into a `simd` matrix `s` via
125+
```swift
126+
let s = m.simd3x2
127+
print(s)
128+
```
129+
resulting in the output
130+
```
131+
simd_double3x2(columns: (SIMD2<Double>(1.0, 4.0), SIMD2<Double>(2.0, 5.0), SIMD2<Double>(3.0, 6.0)))
132132
```
133133
Note that `simd` reverses the role of row and column indices compared to `LANumerics` (and usual mathematical convention).
134134

135+
We can also convert `s` back:
136+
```swift
137+
print(Matrix(s) == m)
138+
```
139+
will yield the output `true`.
140+
135141
## Accessing Matrix Elements and Submatrices
136142

137-
Matrix elements and submatrices can be accessed using familiar notation:
143+
Matrix elements and submatrices can be accessed using familiar notation. Given
138144
```swift
139145
import simd
140146
import LANumerics
141147

142148
var m = Matrix(rows: [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
143149
print(m)
144-
m[2, 1] = 0
145-
print(m)
146-
print(m[0 ... 1, 0 ... 1])
147-
print(m[1 ... 2, 1 ... 2])
148-
m[0 ... 1, 0 ... 1] = m[1 ... 2, 1 ... 2]
149-
print(m)
150150
```
151-
yields the output
151+
with output
152152
```
153153
3x3-matrix:
154154
⎛1.0 2.0 3.0⎞
155155
⎜4.0 5.0 6.0⎟
156156
⎝7.0 8.0 9.0⎠
157+
```
158+
we can access the element at row 2 and column 1 via
159+
```
160+
m[2, 1]
161+
```
162+
which yields `8.0`. We can also set the element at row 2 and column 1 to some value:
163+
```
164+
m[2, 1] = 0
165+
print(m)
166+
```
167+
The output of running this is
168+
```
157169
3x3-matrix:
158170
⎛1.0 2.0 3.0⎞
159171
⎜4.0 5.0 6.0⎟
160172
⎝7.0 0.0 9.0⎠
173+
```
174+
We can also access submatrices of `m`, for example its top-left and bottom-right 2x2 submatrices:
175+
```
176+
print(m[0 ... 1, 0 ... 1])
177+
print(m[1 ... 2, 1 ... 2])
178+
179+
```
180+
This will print
181+
```
161182
2x2-matrix:
162183
⎛1.0 2.0⎞
163184
⎝4.0 5.0⎠
164185
2x2-matrix:
165186
⎛5.0 6.0⎞
166187
⎝0.0 9.0⎠
188+
```
189+
Finally, using the same notation, we can overwrite submatrices of `m`:
190+
```
191+
m[0 ... 1, 0 ... 1] = m[1 ... 2, 1 ... 2]
192+
print(m)
193+
```
194+
This overwrites the top-left 2x2 submatrix of `m` with its bottom-right 2x2 submatrix, yielding:
195+
```
167196
3x3-matrix:
168197
⎛5.0 6.0 3.0⎞
169198
⎜0.0 9.0 6.0⎟
@@ -234,7 +263,7 @@ u * v: 2x2-matrix:
234263
⎝3.0i -3.0 + 5.0i⎠
235264
```
236265

237-
Instead of `u′ * v` one can also use the equivalent, but usually faster expression `u ′* v`:
266+
Instead of `u′ * v` one can also use the equivalent, but faster expression `u ′* v`:
238267
```
239268
print("u′ * v: \(u′ * v)\n")
240269
print("u ′* v: \(u ′* v)\n")
@@ -290,7 +319,7 @@ Matrix(u.vector) * v.vector′: 4x4-matrix:
290319

291320
### Element-wise Operations
292321

293-
Element-wise binary operations like `.+`, `.-`, `.*` and `./` are supported on both vectors and matrices, for example:
322+
Element-wise operations like `.+`, `.-`, `.*` and `./` are supported on both vectors and matrices, for example:
294323

295324
```
296325
u .* v : 2x2-matrix:

0 commit comments

Comments
 (0)