Skip to content

Commit 149959f

Browse files
committed
.
1 parent 71c8142 commit 149959f

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

site/docs/cheatsheet.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import narr.*
2020
| Create a 2D array/matrix | `Matrix(NArray(1.0, 2.0, 3.0, 4.0, 5.0, 6.0), 2, 3)` | `np.array([[1., 2., 3.], [4., 5., 6.]])` | `[1 2 3; 4 5 6]` |
2121
| Array of zeros | `Matrix.zeros((3, 4))` | `np.zeros((3, 4))` | `zeros(3,4)` |
2222
| Array of ones | `Matrix.ones((3, 4))` | `np.ones((3, 4))` | `ones(3,4)` |
23+
| Random array | `NArray.tabulate(12)(_ => scala.util.Random.nextDouble())` | `np.random.rand(12, 1)` | `rand(12,)` |
2324
| Identity matrix | `Matrix.eye(3)` | `np.eye(3)` | `eye(3)` |
2425
| Get array dimensions | `m.rows, m.cols` or `m.shape` | `a.shape` | `size(a)` |
2526
| Number of elements | `m.numel` | `a.size` | `numel(a)` |
@@ -62,7 +63,9 @@ import narr.*
6263
| Matrix multiplication | `m @@ n` or `m.matmul(n)` | `a @ b` | `a * b` |
6364
| Matrix-vector multiply | `m @@ v` | `a @ v` | `a * v` |
6465
| Dot product | `vec.dot(vec2)` | `np.dot(a, b)` or `a @ b` | `dot(a,b)` |
66+
| Outer product | `vec.outer(vec2)` | `np.outer(a, b)` | `a * b.'` or `a(:) * b(:).'` |
6567
| Determinant | `m.det` | `np.linalg.det(a)` | `det(a)` |
68+
| Trace | `m.trace` | `np.trace(a)` | `trace(a)` |
6669
| Matrix inverse | `m.inv` | `np.linalg.inv(a)` | `inv(a)` |
6770
| SVD | `val (U, S, Vt) = svd(m)` | `U, S, Vh = np.linalg.svd(a)` | `[U,S,V]=svd(a)` |
6871
| Pseudo-inverse | `pinv(m)` | `np.linalg.pinv(a)` | `pinv(a)` |
@@ -123,6 +126,12 @@ In vecxt, each of these function has an "in-place" counterpart that returns unit
123126

124127
Such operations can also be called via `tan(vec)`, `exp(matrix)`, etc.
125128

129+
## Broadcasting
130+
131+
Is not supported in an "implicit" fashion. Look at the methods;
132+
133+
`mapRows`, `mapColumns`, `mapRowsToScalar`, `mapColumnsToScalar`, etc. for explict alternatives to broadcasting. Jury still out on this but I never liked implicit broadcasting so it isn't implemented.
134+
126135
## Logical Operations
127136

128137
| Operation | vecxt | NumPy | MATLAB |
@@ -140,14 +149,18 @@ Such operations can also be called via `tan(vec)`, `exp(matrix)`, etc.
140149
| Boolean indexing | `a(a > 2.0)` | `a[a > 0.5]` | `a(a > 0.5)` |
141150
| Count true values | `(a > 2.0).trues` | `np.sum(a > 0.5)` | `sum(a > 0.5)` |
142151

143-
## Array Manipulation
152+
## Array / Matrix Manipulation
144153

145154
| Operation | vecxt | NumPy | MATLAB |
146155
|-----------|-------|-------|--------|
147156
| Extract diagonal | `m.diag` | `np.diag(a)` | `diag(a)` |
148-
| Create diagonal matrix | `Matrix.diag(a)` | `np.diag(v)` | `diag(v,0)` |
157+
| Create diagonal matrix | `Matrix.createDiag(a)` | `np.diag(v)` | `diag(v,0)` |
149158
| Unique values | `vec.unique` | `np.unique(a)` | `unique(a)` |
150159
| Sort | `narr.sort(vec)()` | `np.sort(a)` | `sort(a)` |
160+
| Horizontal concatenation | `m.horzcat(n)` or `horzcat(m, n)` | `np.hstack([a, b])` or `np.concatenate([a,b], axis=1)` | `horzcat(a,b)` or `[a b]` |
161+
| Vertical concatenation | `m.vertcat(n)` or `vertcat(m, n)` | `np.vstack([a, b])` or `np.concatenate([a,b], axis=0)` | `vertcat(a,b)` or `[a; b]` |
162+
| Repeat array | `NArray.tabulate(vec.length * n)(i => vec(i % vec.length))` | `np.tile(a, n)` or `np.repeat(a, n)` | `repmat(a, 1, n)` or `repelem(a, n)` |
163+
151164

152165
## Special Operations
153166

vecxt/src/MatrixHelper.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ object MatrixHelper:
192192
randInt(dim._1, dim._2, 0, 100)
193193
end randInt
194194

195-
inline def diag(v: NArray[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] =
195+
inline def createDiagonal(v: NArray[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] =
196196
val size = v.length
197197
val newArr = NArray.ofSize[Double](size * size)
198198
var j = 0
@@ -207,7 +207,7 @@ object MatrixHelper:
207207
i += 1
208208
end while
209209
Matrix[Double](newArr, (size, size))
210-
end diag
210+
end createDiagonal
211211

212212
end extension
213213

vecxt/src/doublematrix.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import vecxt.arrays.*
77
import vecxt.dimensionExtender.DimensionExtender.*
88
import vecxt.matrix.*
99
import vecxt.matrixUtil.*
10+
import vecxt.MatrixHelper.createDiagonal
1011

1112
import narr.*
1213

@@ -159,6 +160,8 @@ object DoubleMatrix:
159160

160161
inline def *(m2: Matrix[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] = m.hadamard(m2)
161162

163+
inline def kronecker(other: Matrix[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] = ???
164+
162165
inline def hadamard(m2: Matrix[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] =
163166
sameDimMatCheck(m, m2)
164167

vecxt/test/src/matrixhelper.test.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ class MatrixHelperSuite extends FunSuite:
2626

2727
test("Matrix.diag size 1 populates 1x1 matrix"):
2828
val diagonal = NArray[Double](42.0)
29-
val diagMatrix = Matrix.diag(diagonal)
29+
val diagMatrix = Matrix.createDiagonal(diagonal)
3030
assertDiagonalMatrix(diagonal, diagMatrix)
3131

3232
test("Matrix.diag size 3 builds zero off-diagonal entries"):
3333
val diagonal = NArray[Double](1.0, -2.5, 7.75)
34-
val diagMatrix = Matrix.diag(diagonal)
34+
val diagMatrix = Matrix.createDiagonal(diagonal)
3535
assertDiagonalMatrix(diagonal, diagMatrix)
3636

3737
test("Matrix.diag size 7 handles longer vectors"):
3838
val diagonal = NArray.tabulate[Double](7)(i => (i + 1) * 0.5)
39-
val diagMatrix = Matrix.diag(diagonal)
39+
val diagMatrix = Matrix.createDiagonal(diagonal)
4040
assertDiagonalMatrix(diagonal, diagMatrix)
4141
end MatrixHelperSuite

0 commit comments

Comments
 (0)