1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// #![feature(auto_traits, negative_impls)]
//! An extremely minimal super static type safe implementation of matrix types.
//!
//! While [`ndarray`](https://docs.rs/ndarray/latest/ndarray/) offers no compile time type checking
//! on dimensionality and [`nalgebra`](https://docs.rs/nalgebra/latest/nalgebra/) offers some
//! finnicky checking, this offers the maximum possible checking.
//!
//! When performing the addition of a [`MatrixDxS`] (a matrix with a known number of columns at
//! compile time) and a [`MatrixSxD`] (a matrix with a known number of rows at compile time) you
//! get a [`MatrixSxS`] (a matrix with a known number of rows and columns at compile time) since
//! now both the number of rows and columns are known at compile time. This then allows this
//! infomation to propagate through your program providing excellent compile time checking.
//!
//! That being said... I made this in a weekend and there is a tiny amount of functionality.
//!
//! An example of how types will propagate through a program:
//! ```
//! #![allow(incomplete_features)]
//! #![feature(generic_const_exprs)]
//! use static_la::*;
//! // MatrixSxS<i32,2,3>
//! let a = MatrixSxS::from([[1,2,3],[4,5,6]]);
//! // MatrixDxS<i32,3>
//! let b = MatrixDxS::from(vec![[2,2,2],[3,3,3]]);
//! // MatrixSxS<i32,2,3>
//! let c = (a.clone() + b.clone()) - a.clone();
//! // MatrixDxS<i32,3>
//! let d = c.add_rows(b);
//! // MatrixSxS<i32,4,3>
//! let e = MatrixSxS::from([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]);
//! // MatrixSxS<i32,4,6>
//! let f = d.add_columns(e);
//! ```
//!
//! In this example the only operations which cannot be fully checked at compile time are:
//! 1. `a.clone() + b.clone()`
//! 2. `d.add_columns(e)`
//!
//!
//! **You must include `#![feature(generic_const_exprs)]` when using this library otherwise you will get a compiler error.**
//!
//! ### Construction
//! ```
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! use std::convert::TryFrom;
//! use static_la::*;
//! // From shaped arrays/vecs
//! let dxd = MatrixDxD::try_from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
//! let dxs = MatrixDxS::from(vec![[1, 2, 3], [4, 5, 6]]);
//! let sxd = MatrixSxD::try_from([vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
//! let sxs = MatrixSxS::<i32,2,3>::from([[1, 2, 3], [4, 5, 6]]);
//! // From a given shape and array/vec
//! let dxd = MatrixDxD::try_from((2,3,vec![1, 2, 3, 4, 5, 6])).unwrap();
//! let dxs = MatrixDxS::<_,2>::try_from((3,vec![1, 2, 3,4, 5, 6])).unwrap();
//! let sxd = MatrixSxD::<_,3>::try_from((2,vec![1, 2, 3, 4, 5, 6])).unwrap();
//! let sxs = MatrixSxS::<i32,2,3>::from([1, 2, 3, 4, 5, 6]);
//! // From a given shape and slice
//! let dxd = MatrixDxD::try_from((2,3,&[1, 2, 3, 4, 5, 6])).unwrap();
//! let dxs = MatrixDxS::<_,2>::try_from((3,&[1, 2, 3,4, 5, 6])).unwrap();
//! let sxd = MatrixSxD::<_,3>::try_from((2,&[1, 2, 3, 4, 5, 6])).unwrap();
//! let sxs = MatrixSxS::<i32,2,3>::from(&[1, 2, 3, 4, 5, 6]);
//! // ┌───────┐
//! // │ 1 2 3 │
//! // │ 4 5 6 │
//! // └───────┘
//! // From a given shape and value
//! let dxd = MatrixDxD::from((2,3,5));
//! let dxs = MatrixDxS::<_,3>::from((2,5));
//! let sxd = MatrixSxD::<_,2>::from((3,5));
//! let sxs = MatrixSxS::<i32,2,3>::from(5);
//! // ┌───────┐
//! // │ 5 5 5 │
//! // │ 5 5 5 │
//! // └───────┘
//! ```
//! *This requires the `distribution` feature.*
//! ```ignore
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! // From a given shape and with values sampled from a given distribution
//! use rand::distributions::{Uniform,Standard};
//! use static_la::*;
//! let dxd = MatrixDxD::<i32>::distribution(2,3,Uniform::from(0i32..10i32));
//! let dxs = MatrixDxS::<f32,3>::distribution(2,Standard);
//! let sxd = MatrixSxD::<f32,2>::distribution(3,Standard);
//! let sxs = MatrixSxS::<f32,2,3>::distribution(Standard);
//! ```
//! ### Indexing
//! ```
//! use static_la::MatrixDxS;
//! let a = MatrixDxS::from(vec![[1, 2, 3], [4, 5, 6]]);
//! assert_eq!(a[(0,0)], 1);
//! assert_eq!(a[(0,1)], 2);
//! assert_eq!(a[(0,2)], 3);
//! assert_eq!(a[(1,0)], 4);
//! assert_eq!(a[(1,1)], 5);
//! assert_eq!(a[(1,2)], 6);
//! ```
//! ### Expansion
//! ```
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! use std::convert::TryFrom;
//! use static_la::*;
//! let a = MatrixDxD::try_from(vec![vec![1, 2]]).unwrap();
//! // ┌─────┐
//! // │ 1 2 │
//! // └─────┘
//! let b = a.add_rows(MatrixDxS::from(vec![[4, 5]]));
//! // ┌─────┐
//! // │ 1 2 │
//! // │ 4 5 │
//! // └─────┘
//! let c = b.add_columns(MatrixSxS::from([[3], [6]]));
//! // ┌───────┐
//! // │ 1 2 3 │
//! // │ 4 5 6 │
//! // └───────┘
//! assert_eq!(c,MatrixSxD::try_from([vec![1, 2, 3], vec![4, 5, 6]]).unwrap());
//! ```
//! ### Arithmetic
//! Most traits from [`std::ops`] are implemented for both matrix and scalar types.
//! ```
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! use static_la::MatrixSxS;
//! let a = MatrixSxS::from([[1, 2, 3], [4, 5, 6]]);
//! let b = a + 7;
//! let mut c = b + MatrixSxS::from([[10, 20, 30], [40, 50, 60]]);
//! c += 3;
//! c += MatrixSxS::from([[10, 20, 30], [40, 50, 60]]);
//! ```
//! ### Slicing
//! ```
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! use std::convert::TryFrom;
//! use static_la::*;
//! let a = MatrixDxD::try_from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
//! assert_eq!(a.slice_dxd((0..1, 0..2)), MatrixDxD::try_from(vec![vec![&1, &2]]).unwrap());
//! assert_eq!(a.slice_dxs::<{ 0..2 }>(0..1), MatrixDxS::from(vec![[&1, &2]]));
//! assert_eq!(a.slice_sxd::<{ 0..1 }>(0..2), MatrixSxD::try_from([vec![&1, &2]]).unwrap());
//! assert_eq!(a.slice_sxs::<{ 0..1 }, { 0..2 }>(), MatrixSxS::from([[&1, &2]]));
//! assert_eq!(a.slice_sxs::<{ 0..1 }, { 0..2 }>(), MatrixDxD::try_from(vec![vec![&1, &2]]).unwrap());
//! ```
//! ### Transpose
//! ```
//! use static_la::MatrixSxS;
//! let a = MatrixSxS::from([[1, 2, 3], [4, 5, 6]]);
//! let b = a.transpose_ref(); // Please see docs
//! let c = a.transpose();
//! assert_eq!(c,MatrixSxS::<i32,3,2>::from([[1, 4], [2, 5],[3, 6]]));
//! ```
//! ### BLAS
//! ```ignore
//! # #![allow(incomplete_features)]
//! # #![feature(generic_const_exprs)]
//! use static_la::MatrixSxS;
//! let a = MatrixSxS::<f32, 2, 3>::from([[1., 3., 5.], [2., 4., 6.]]);
//! let b = MatrixSxS::<f32, 3, 2>::from([[7., 10.], [8., 11.], [9., 12.]]);
//! let mut c = MatrixSxS::<f32, 2, 2>::from([[0., 0.], [0., 0.]]);
//! static_la::blas::sgemm(false, false, 1., &a, &b, 1., &mut c);
//! assert_eq!(c, MatrixSxS::<f32, 2, 2>::from([[76., 103.], [100., 136.]]));
//! ```
//! The basic matrix multiply for `f32`s and `f64`s uses `sgemm` and `dgemm`.
/// [`std::ops::Add`] Arithmetic addition operations.
/// [`std::ops::AddAssign`] Arithmetic addition operations.
/// Functionality to expand matrices.
pub use AddRows;
/// Functionality to expand matrices.
pub use AddColumns;
/// [`std::fmt::Display`] Format implementations.
/// [`std::ops::Div`] Arithmetic division operations.
/// [`std::ops::DivAssign`] Arithmetic division operations.
/// [`std::convert::From`] Value-to-value conversions.
/// [`std::ops::Index`] Indexing operations.
/// Iterations functionality.
/// Matrix multiplication functionality.
pub use Matmul;
/// [`std::ops::Mul`] Arithmetic multiplication operations.
/// [`std::ops::MulAssign`] Arithmetic multiplication operations.
/// [`std::cmp::PartialEq`] Partial equality comparison operations.
/// Slicing functionality.
pub use ;
/// Implementations relating to dimensions of matrices.
/// Constructing matrices with random values functionality.
/// [`std::ops::Sub`] Arithmetic subtraction operations.
/// [`std::ops::SubAssign`] Arithmetic subtraction operations.
/// Implementations relating to summations over matrices.
/// Transpose functionality.
pub use *;
/// [`std::ops::BitAnd`] Bitwise AND operation.
/// [`std::ops::BitAndAssign`] Bitwise AND assignment operation.
/// [`std::ops::BitOr`] Bitwise OR operation.
/// [`std::ops::BitOrAssign`] Bitwise OR assignment operation.
/// [`std::ops::BitXor`] Bitwise XOR operation.
/// [`std::ops::BitXorAssign`] Bitwise XOR assignment operation.
/// BLAS operations.
///
/// There are too many combinations for the BLAS operations (sgemm has more than 64) to manually
/// write the implementations to allow for compile time checking. So the checking and type
/// joining does not occur with these functions.
///
/// If you have any idea how to do this better please open an issue, discussion or pull request.
///
/// There is nothing in the way of full support other than my own motivation, I plan to implement all BLAS functionality moving forward.
/// [`std::ops::Neg`] Negation operations.
/// [`std::ops::Not`] Bitwise NOT operations.
/// [`std::ops::Rem`] Remainder functionality.
/// [`std::ops::RemAssign`] Remainder assign functionality.
/// [`std::convert::TryFrom`] Fallible value-to-value conversions.
// Matrix types
// --------------------------------------------------
/// A `dynamic x dynamic` matrix where neither dimension is known at compile time.
/// ```
/// use std::convert::TryFrom;
/// let _ = static_la::MatrixDxD::try_from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
/// ```
/// A `dynamic x static` matrix where the columns dimension is known at compile time.
/// ```
/// let _ = static_la::MatrixDxS::from(vec![[1, 2, 3], [4, 5, 6]]);
/// ```
/// A `static x dynamic` matrix where the rows dimension is known at compile time.
/// ```
/// use std::convert::TryFrom;
/// let _ = static_la::MatrixSxD::try_from([vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
/// ```
/// A `static x static` matrix where both dimensions are known at compile time.
/// ```
/// let _ = static_la::MatrixSxS::<i32,2,3>::from([[1, 2, 3], [4, 5, 6]]);
/// ```
; ROWS * COLUMNS]:,
// Vector aliases
// --------------------------------------------------
/// A matrix with 1 column and a dynamic number of rows.
///
/// E.g.
/// ```text
/// ┌───┐
/// │ 1 │
/// │ 2 │
/// │ 3 │
/// └───┘
/// ```
pub type ColumnVectorD<T> = ;
/// A matrix with 1 column and a static number of rows.
///
/// E.g.
/// ```text
/// ┌───┐
/// │ 1 │
/// │ 2 │
/// │ 3 │
/// └───┘
/// ```
pub type ColumnVectorS<T, const ROWS: usize> = ;
/// A matrix with 1 row and a dynamic number of columns.
///
/// E.g.
/// ```text
/// ┌───────┐
/// │ 1 2 3 │
/// └───────┘
/// ```
pub type RowVectorD<T> = ;
/// A matrix with 1 row and a static number of columns.
///
/// E.g.
/// ```text
/// ┌───────┐
/// │ 1 2 3 │
/// └───────┘
/// ```
pub type RowVectorS<T, const COLUMNS: usize> = ;