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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! # matrix::vector
//!
//! Definitions of column vectors and row vectors,
//! along with related functions.
//!
//! Default vectors are column vectors,
//! so most functions are implemented only for column vectors.
//! For row vectors, you can first transpose them into column vectors
//! and then use the corresponding functions.
use ;
use crate::;
/// A row vector is a matrix with a single row.
pub type VectorR<N, const C: usize> = ;
/// A column vector is a matrix with a single column.
pub type VectorC<N, const R: usize> = ;
/// Used to obtain a reference to the element
/// at the corresponding position in the column vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{index_c, VectorC},
/// number::instances::word8::Word8,
/// };
///
/// fn main() {
/// let v1: VectorC<Word8, 3> = VectorC::of(&[Word8::of(1), Word8::of(2), Word8::of(3)]).unwrap();
/// assert_eq!(index_c(&v1, 2), &Word8::of(2));
/// }
/// ```
/// Used to obtain a reference to the element
/// at the corresponding position in the row vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{index_r, VectorR},
/// number::instances::word8::Word8,
/// };
///
/// fn main() {
/// let v1: VectorR<Word8, 3> = VectorR::of(&[Word8::of(1), Word8::of(2), Word8::of(3)]).unwrap();
/// assert_eq!(index_r(&v1, 2), &Word8::of(2));
/// }
/// ```
/// Used to obtain the basis vector.
///
/// The `index` represents the index of the basis vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{basis_vector, VectorC},
/// number::instances::int8::Int8,
/// };
///
/// pub fn main() {
/// let e1_a: VectorC<Int8, 3> = VectorC::of(&[Int8::of(0), Int8::of(1), Int8::of(0)]).unwrap();
/// let e1_b = basis_vector::<Int8, 3>(2);
/// assert_eq!(e1_a, e1_b);
/// }
/// ```
/// Calculate the dot product of two column vectors.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{dot_product, VectorC},
/// number::instances::int8::Int8,
/// };
///
/// fn main() {
/// let v1: VectorC<Int8, 3> = VectorC::of(&[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2: VectorC<Int8, 3> = VectorC::of(&[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// assert_eq!(dot_product(v1, v2), Int8::of(32));
/// }
/// ```
/// Calculate the cross product of two three-dimensional column vectors.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{cross_product, VectorC},
/// number::instances::int8::Int8,
/// };
///
/// fn main() {
/// let v1: VectorC<Int8, 3> = VectorC::of(&[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2: VectorC<Int8, 3> = VectorC::of(&[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// assert!(cross_product(v1, v2)
/// .equals(&VectorC::of(&[Int8::of(-3), Int8::of(6), Int8::of(-3)]).unwrap()));
/// }
/// ```
/// Construct a matrix using one column vector and one row vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::{
/// matrix::Matrix,
/// vector::{layer_product, VectorC, VectorR},
/// },
/// number::instances::int8::Int8,
/// };
///
/// fn main() {
/// let v1: VectorC<Int8, 3> = VectorC::of(&[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2: VectorR<Int8, 3> = VectorR::of(&[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// let data = [
/// 4i8, 5i8, 6i8, // row1
/// 8i8, 10i8, 12i8, // row2
/// 12i8, 15i8, 18i8, // row3
/// ]
/// .map(|e| Int8::of(e));
/// let m = Matrix::<Int8, 3, 3>::of(&data).unwrap();
/// assert_eq!(layer_product(v1, v2), m);
/// }
/// ```
/// Calculate the convolution of two column vectors.
///
/// # Panics
///
/// This function requires the use of the `#![feature(generic_const_exprs)]`.
///
/// # Examples
///
/// ```rust
/// #![allow(incomplete_features)]
/// #![feature(generic_const_exprs)]
///
/// use rmatrix_ks::{
/// matrix::vector::{convolution, VectorC},
/// number::instances::int8::Int8,
/// };
///
/// fn main() {
/// let v1: VectorC<Int8, 3> = VectorC::of(&[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2: VectorC<Int8, 2> = VectorC::of(&[Int8::of(4), Int8::of(5)]).unwrap();
/// let cv = VectorC::<Int8, 4>::of(&[4, 13, 22, 15].map(|e| Int8::of(e))).unwrap();
/// assert_eq!(convolution(v1, v2), cv);
/// }
/// ```
+ R2 - 1 }>
where
N: Number,
/// Calculate the Euclidean norm.
///
/// Aka L2-norm.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{euclidean_norm, VectorC},
/// number::{
/// instances::float::Float,
/// traits::{floating::Floating, zero::Zero},
/// },
/// };
///
/// fn main() {
/// let v: VectorC<Float, 3> =
/// VectorC::of(&[Float::of(1.0), Float::of(2.0), Float::of(3.0)]).unwrap();
/// assert!((euclidean_norm(&v) - Float::of(14.0).square_root()).is_zero());
/// }
/// ```
/// Calculate the maximum norm.
///
/// Aka L_inf-norm.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{maximum_norm, VectorC},
/// number::instances::int8::Int8,
/// };
///
/// fn main() {
/// let v = VectorC::<Int8, 3>::of(&[Int8::of(2), Int8::of(-5), Int8::of(3)]).unwrap();
/// assert_eq!(maximum_norm(&v), Int8::of(5))
/// }
/// ```
/// Calculate the root mean square of the column vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{root_mean_square, VectorC},
/// number::{
/// instances::float::Float,
/// traits::{floating::Floating, zero::Zero},
/// },
/// };
///
/// fn main() {
/// let v: VectorC<Float, 3> =
/// VectorC::of(&[Float::of(1.0), Float::of(2.0), Float::of(3.0)]).unwrap();
/// assert!((root_mean_square(&v) - Float::of(14.0 / 3.0).square_root()).is_zero());
/// }
/// ```
/// Calculate the angle between two vectors.
///
/// Expressed in radians.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
/// matrix::vector::{angle_between, VectorC},
/// number::{instances::double::Double, traits::zero::Zero},
/// };
///
/// fn main() {
/// let v1: VectorC<Double, 3> =
/// VectorC::of(&[Double::of(1.0), Double::of(5.0), Double::of(4.0)]).unwrap();
/// let v2: VectorC<Double, 3> =
/// VectorC::of(&[Double::of(8.0), Double::of(-4.0), Double::of(3.0)]).unwrap();
/// let angle = angle_between(&v1, &v2);
/// assert!(angle.is_some());
/// assert!((angle.unwrap() - Double::of(core::f64::consts::PI / 2.0)).is_zero());
///
/// // angle between a vector and itself is zero
/// let self_angle = angle_between(&v1, &v1);
/// assert!(self_angle.is_some());
/// assert!(self_angle.unwrap().is_zero());
///
/// // zero-vector has no angle with any other vector
/// let v3 = VectorC::of(&[Double::zero(), Double::zero(), Double::zero()]).unwrap();
/// assert!(angle_between(&v1, &v3).is_none());
/// }
/// ```