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
//! Basic vector and matrix-vector operations, including `Ax`, `Ax + βy`, Givens rotations, argmax, and rank-1 updates
//!
//! # Matrix-Vector Operations
//!
//! ```rust
//! use mdarray::tensor;
//! use mdarray_linalg::prelude::*;
//! use mdarray_linalg::Naive;
//!
//! // Create a 3x3 matrix and a vector
//! let a = tensor![[1., 2., 3.],
//! [4., 5., 6.],
//! [7., 8., 9.]];
//! let x = tensor![1., 1., 1.];
//!
//! // Basic matrix-vector multiplication: y = A·x
//! let y = Naive.matvec(&a, &x).eval();
//! assert_eq!(y, tensor![6., 15., 24.]);
//!
//! // Scaled operation: y = 2·A·x
//! let y_scaled = Naive.matvec(&a, &x).scale(2.).eval();
//! assert_eq!(y_scaled, tensor![12., 30., 48.]);
//!
//! // Write result to existing vector: y := α·A·x
//! let mut y_write = tensor![0., 0., 0.];
//! Naive.matvec(&a, &x).scale(2.).write(&mut y_write);
//! assert_eq!(y_write, tensor![12., 30., 48.]);
//!
//! // Add to vector: y := A·x + y
//! let mut y_add = tensor![1., 1., 1.];
//! Naive.matvec(&a, &x).add_to_vec(&mut y_add);
//! assert_eq!(y_add, tensor![7., 16., 25.]);
//!
//! // Scaled addition: y := α·A·x + β·y
//! let mut y_axpy = tensor![1., 1., 1.];
//! Naive.matvec(&a, &x).add_to_scaled_vec(&mut y_axpy, 2.);
//! assert_eq!(y_axpy, tensor![8., 17., 26.]);
//! ```
//!
//! # Outer Products and Rank-1 Updates
//!
//! ```rust
//! use mdarray::tensor;
//! use mdarray_linalg::prelude::*;
//! use mdarray_linalg::Naive;
//!
//! // Create two vectors
//! let x = tensor![1., 2.];
//! let y = tensor![1., 10., 100.];
//!
//! // Basic outer product: A = x ⊗ y
//! let a = Naive.outer(&x, &y).eval();
//! assert_eq!(a, tensor![[1., 10., 100.],
//! [2., 20., 200.]]);
//!
//! // Scaled outer product: A = β·(x ⊗ y)
//! let a_scaled = Naive.outer(&x, &y).scale(2.).eval();
//! assert_eq!(a_scaled, tensor![[2., 20., 200.],
//! [4., 40., 400.]]);
//!
//! // Write to existing matrix: A := β·(x ⊗ y)
//! let mut a_write = tensor![[0., 0., 0.],
//! [0., 0., 0.]];
//! Naive.outer(&x, &y).scale(2.).write(&mut a_write);
//! assert_eq!(a_write, tensor![[2., 20., 200.],
//! [4., 40., 400.]]);
//!
//! // Rank-1 update: A := β·(x ⊗ y) + A
//! let mut a_update = tensor![[1., 1., 1.],
//! [1., 1., 1.]];
//! Naive.outer(&x, &y).scale(2.).add_to(&mut a_update);
//! assert_eq!(a_update, tensor![[3., 21., 201.],
//! [5., 41., 401.]]);
//! ```
//!
//! # Complex Number Support
//!
//! ```rust
//! use mdarray::tensor;
//! use mdarray_linalg::prelude::*;
//! use mdarray_linalg::Naive;
//! use num_complex::Complex64;
//!
//! // Complex outer product
//! let x = tensor![Complex64::new(1., 1.), Complex64::new(2., 0.)];
//! let y = tensor![Complex64::new(1., 0.), Complex64::new(0., 1.)];
//! let a = Naive.outer(&x, &y).eval();
//! assert_eq!(a[[0, 0]], Complex64::new(1., 1.));
//! assert_eq!(a[[0, 1]], Complex64::new(-1., 1.));
//!
//! ```
//! # Argmax
//!
//! ```rust
//! use mdarray::tensor;
//! use mdarray_linalg::prelude::*;
//! use mdarray_linalg::Naive;
//!
//! // Find index of maximum value in 1D array
//! let x = tensor![1., 5., 3., 8., 2.];
//! let idx = Naive.argmax(&x).unwrap();
//! assert_eq!(idx, vec![3]); // Maximum is at index 3
//!
//! // Find index in 2D array (returns multi-dimensional index)
//! let a = tensor![[0., 1., 2.],
//! [3., 4., 5.]];
//! let idx = Naive.argmax(&a.view(.., ..).into_dyn()).unwrap();
//! assert_eq!(idx, vec![1, 2]); // Maximum is at position [1, 2]
//!
//! // Find element with largest absolute value
//! let y = tensor![1., -6., 3., -2., 5.];
//! let idx = Naive.argmax_abs(&y).unwrap();
//! assert_eq!(idx, vec![1]); // -6 has largest absolute value
//!
//! // Write result to reusable buffer
//! let mut output = Vec::new();
//! let success = Naive.argmax_write(&x, &mut output);
//! assert!(success);
//! assert_eq!(output, vec![3]);
//! ```
//! # Vector Operations
//!
//! ```rust
//! use mdarray::tensor;
//! use mdarray_linalg::prelude::*;
//! use mdarray_linalg::Naive;
//! use num_complex::Complex64;
//!
//! // Scaled vector addition: y := α·x + y
//! let x = tensor![1., 2., 3.];
//! let mut y = tensor![1., 1., 1.];
//! Naive.add_to_scaled(2.0, &x, &mut y);
//! assert_eq!(y, tensor![3., 5., 7.]); // y = 2·x + y
//!
//! // Dot product: ∑xᵢyᵢ
//! let x = tensor![1., 2., 3.];
//! let y = tensor![2., 4., 6.];
//! let result = Naive.dot(&x, &y);
//! assert_eq!(result, 28.0); // 1*2 + 2*4 + 3*6 = 28
//!
//! // Conjugated dot product with complex numbers: ∑(conj(xᵢ)·yᵢ)
//! let x = tensor![Complex64::new(1., 2.), Complex64::new(2., 3.)];
//! let y = tensor![Complex64::new(3., 4.), Complex64::new(4., 5.)];
//! let result = Naive.dotc(&x, &y);
//! // conj(1+2i)*(3+4i) + conj(2+3i)*(4+5i)
//! let expected = x[[0]].conj() * y[[0]] + x[[1]].conj() * y[[1]];
//! assert_eq!(result, expected);
//!
//! // L2 norm (Euclidean): √(∑|xᵢ|²)
//! let x = tensor![3., 4.];
//! let norm = Naive.norm2(&x);
//! assert_eq!(norm, 5.0); // √(9 + 16) = 5
//!
//! // L1 norm (Manhattan): ∑|xᵢ|
//! let x = tensor![Complex64::new(1., 2.), Complex64::new(2., 3.)];
//! let norm = Naive.norm1(&x);
//! // |1+2i| + |2+3i| = (|1|+|2|) + (|2|+|3|) = 8
//! assert_eq!(norm, 8.0);
//! ```
use ;
/// Matrix-vector multiplication and transformations