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
//! # Multidimensional array for Rust
//!
//! ## Overview
//!
//! The mdarray crate provides a multidimensional array for Rust. Its main target
//! is for numeric types, however generic types are supported as well. The purpose
//! is to provide a generic container type that is simple and flexible to use,
//! with interworking to other crates for e.g. BLAS/LAPACK functionality.
//!
//! Here are the main features of mdarray:
//!
//! - Dense array type, where the rank and element order is known at compile time.
//! - Column-major and row-major element order.
//! - Subarrays (views) can be created with arbitrary shapes and strides.
//! - Standard Rust mechanisms are used for e.g. slices, indexing and iteration.
//!
//! The design is inspired from the Rust ndarray, nalgebra and bitvec crates,
//! the proposed C++ mdarray and mdspan types, and multidimensional arrays in
//! Julia and Matlab.
//!
//! ## Array types
//!
//! The base types for multidimensional arrays are `GridBase` and `SpanBase`,
//! similar to the Rust `Vec` and `slice` types.
//!
//! `GridBase` consists of a buffer for element storage and information about
//! the array layout. The buffer can either own the storage like `Vec`, or refer
//! to a parent array. The latter case occurs for example when creating a view
//! of a larger array without duplicating elements.
//!
//! `SpanBase` is used as a generic array reference. It consists of a pointer
//! to an internal structure that holds the buffer and the layout. It is useful
//! for function parameters where the same `SpanBase` type can refer to either
//! an owned array or an array view.
//!
//! The array layout describes how elements are stored in memory. The layout
//! is parameterized by the rank (i.e. the number of dimensions), the element
//! order and the storage format. It contains the shape (i.e. the size in each
//! dimension), and the strides per dimension if needed.
//!
//! The storage format is `Dense` if elements are stored contiguously without gaps.
//! In this case, the strides are calculated from the shape and not stored as
//! part of the layout. The format is `General` if each dimension can have
//! arbitrary stride except for the innermost one, which has unit stride. It is
//! compatible with the BLAS/LAPACK general matrix storage.
//!
//! The format is `Flat` if the innermost dimension can have arbitrary stride
//! and the other dimensions must follow in order, allowing for linear indexing.
//! The format is `Strided` if all dimensions can have arbitrary strides.
//!
//! The element order is `ColumnMajor` for Fortran order where the innermost
//! dimension is the innermost one, or `RowMajor` for the opposite C order.
//! Besides indexing for element access, the order affects how iteration is done
//! over multiple dimensions.
//!
//! The following type aliases are provided:
//!
//! | Alias | Description |
//! | -------------------------------------- | ----------------------------------- |
//! | `Grid<T, const N: usize, A = Global>` | Dense array with column-major order |
//! | `CGrid<T, const N: usize, A = Global>` | Dense array with row-major order |
//! | `Span<T, const N: usize, F = Dense>` | Array span with column-major order |
//! | `CSpan<T, const N: usize, F = Dense>` | Array span with row-major order |
//!
//! ## Indexing and views
//!
//! Scalar indexing is done using the normal square-bracket index operator and
//! an array of `usize` per dimension as index.
//!
//! If the array layout type supports linear indexing, a scalar `usize` can also
//! be used as index. If the array layout type supports slice indexing, a range
//! can be used as index to select a slice.
//!
//! An array view can be created with the `view` and `view_mut` methods and a
//! tuple of indices per dimension as argument. Each index can be either a range
//! or `usize`. The resulting storage format depends on both the format inferred
//! from the indices and the input format.
//!
//! ## Iteration
//!
//! If the array layout type supports linear indexing, an iterator can be created
//! with the `iter`, `iter_mut` and `into_iter` methods like `Vec` and `slice`.
//!
//! For multidimensional arrays, indexing over a single dimension is done
//! with the `outer_iter`/`outer_iter_mut`, `inner_iter`/`inner_iter_mut` and
//! `axis_iter`/`axis_iter_mut` methods. The iterators give array views of
//! the remaining dimensions.
//!
//! If linear indexing is possible but the array layout type is not known, the
//! `reformat`, `reformat_mut` and `into_format` methods can be used to change
//! format with runtime checking. Alternatively, the `flatten`, `flatten_mut`
//! and `into_flattened` methods can be used to change to a one-dimensional
//! array.
//!
//! ## Operators
//!
//! Arithmetic, logical, negation, comparison and compound assignment operators
//! are supported for arrays. For arithmetic, logical and negation operators,
//! at most one parameter can be passed by value. If all parametes are passed by
//! reference, a new array is allocated for the result. For comparison operators,
//! the parameters are always passed by reference.
//!
//! Scalar parameters must be passed using the `fill` function to wrap the value
//! in a `Fill` struct. If the type does not implement the `Copy` trait, the
//! parameter must be passed by reference.
//!
//! Note that for complex calculations it can be more efficient to use iterators
//! and element-wise operations to reduce memory accesses.
//!
//! ## Example
//!
//! This example implements matrix multiplication and addition `C = A * B + C`.
//! The matrices use column-major ordering, and the inner loop runs over one column
//! in `A` and `C`. By using iterators the array bounds checking is avoided, and
//! the compiler is able to vectorize the inner loop.
//!
//! ```
//! use mdarray::{Grid, Span, SubGrid};
//!
//! fn matmul(a: &Span<f64, 2>, b: &Span<f64, 2>, c: &mut Span<f64, 2>) {
//! assert!(c.shape() == [a.size(0), b.size(1)] && a.size(1) == b.size(0), "shape mismatch");
//!
//! for (mut cj, bj) in c.outer_iter_mut().zip(b.outer_iter()) {
//! for (ak, bkj) in a.outer_iter().zip(bj.iter()) {
//! for (cij, aik) in cj.iter_mut().zip(ak.iter()) {
//! *cij = aik.mul_add(*bkj, *cij);
//! }
//! }
//! }
//! }
//!
//! let a = SubGrid::from(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]);
//! let b = SubGrid::from(&[[0.0, 1.0], [1.0, 1.0]]);
//!
//! let mut c = Grid::from_elem([3, 2], 0.0);
//!
//! matmul(&a, &b, &mut c);
//!
//! println!("{c:?}");
//! # assert!(c == SubGrid::from(&[[4.0, 5.0, 6.0], [5.0, 7.0, 9.0]]));
//! ```
//!
//! This will produce the result `[[4.0, 5.0, 6.0], [5.0, 7.0, 9.0]]`.
/// Module for array span and view indexing, and for array axis subarray types.
/// Module for array axis and linear array span iterators.
use Global;
use Global;
pub use ;
pub use ;
pub use ;
pub use Layout;
pub use ;
pub use ;
pub use SpanBase;
/// Dense multidimensional array with column-major element order.
pub type Grid<T, const N: usize, A = Global> = ;
/// Dense multidimensional array with row-major element order.
pub type CGrid<T, const N: usize, A = Global> = ;
/// Multidimensional array span with column-major element order.
pub type Span<T, const N: usize, F = Dense> = ;
/// Multidimensional array span with row-major element order.
pub type CSpan<T, const N: usize, F = Dense> = ;