pub struct Matrix { /* private fields */ }
Expand description
A matrix structure.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(row_count: usize, col_count: usize) -> Self
pub fn new(row_count: usize, col_count: usize) -> Self
Creates a matrix with the number of rows and the number of columns.
Sourcepub fn new_with_elems(row_count: usize, col_count: usize, elems: &[f32]) -> Self
pub fn new_with_elems(row_count: usize, col_count: usize, elems: &[f32]) -> Self
Creates a matrix with the number of rows, the number of columns, and the elements.
Examples found in repository?
More examples
13fn create_matrix(n: usize, m: usize, is_scalar: bool) -> Matrix
14{
15 let mut elems: Vec<f32> = vec![0.0f32; n * m];
16 let scalar = if is_scalar {
17 (n * m) as f32
18 } else {
19 1.0f32
20 };
21 for i in 0..n {
22 for j in 0..m {
23 elems[m * i + j] = ((m * i + j) as f32) * scalar;
24 }
25 }
26 Matrix::new_with_elems(n, m, elems.as_slice())
27}
Sourcepub fn new_with_elem_vecs(elem_vecs: &[Vec<f32>]) -> Self
pub fn new_with_elem_vecs(elem_vecs: &[Vec<f32>]) -> Self
Creates a matrix with the vector of rows.
Sourcepub fn row_count(&self) -> usize
pub fn row_count(&self) -> usize
Returns the number of matrix rows.
Examples found in repository?
10fn main()
11{
12 let a = matrix![
13 [1.0, 2.0, 3.0],
14 [4.0, 5.0, 6.0],
15 [7.0, 8.0, 9.0]
16 ];
17 let b = matrix![
18 [1.0, 4.0, 7.0],
19 [2.0, 5.0, 8.0],
20 [3.0, 6.0, 9.0]
21 ];
22 let c = a * b;
23 let elems = c.elems();
24 for i in 0..c.row_count() {
25 for j in 0..c.col_count() {
26 print!("\t{}", elems[i * c.col_count() + j]);
27 }
28 println!("");
29 }
30}
Sourcepub fn col_count(&self) -> usize
pub fn col_count(&self) -> usize
Returns the number of matrix columns.
Examples found in repository?
10fn main()
11{
12 let a = matrix![
13 [1.0, 2.0, 3.0],
14 [4.0, 5.0, 6.0],
15 [7.0, 8.0, 9.0]
16 ];
17 let b = matrix![
18 [1.0, 4.0, 7.0],
19 [2.0, 5.0, 8.0],
20 [3.0, 6.0, 9.0]
21 ];
22 let c = a * b;
23 let elems = c.elems();
24 for i in 0..c.row_count() {
25 for j in 0..c.col_count() {
26 print!("\t{}", elems[i * c.col_count() + j]);
27 }
28 println!("");
29 }
30}
Sourcepub fn is_transposed(&self) -> bool
pub fn is_transposed(&self) -> bool
Returns true
if the matrix is transposed, otherwise false
.
This method indeed returns the transpose flag of matrix that is changed by
transpose
.
Sourcepub fn elems(&self) -> Vec<f32>
pub fn elems(&self) -> Vec<f32>
Returns the matrix elements.
Examples found in repository?
10fn main()
11{
12 let a = matrix![
13 [1.0, 2.0, 3.0],
14 [4.0, 5.0, 6.0],
15 [7.0, 8.0, 9.0]
16 ];
17 let b = matrix![
18 [1.0, 4.0, 7.0],
19 [2.0, 5.0, 8.0],
20 [3.0, 6.0, 9.0]
21 ];
22 let c = a * b;
23 let elems = c.elems();
24 for i in 0..c.row_count() {
25 for j in 0..c.col_count() {
26 print!("\t{}", elems[i * c.col_count() + j]);
27 }
28 println!("");
29 }
30}
More examples
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(l, m);
73 let now = Instant::now();
74 let c = a * b;
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(m, l);
73 let now = Instant::now();
74 let c = a * b.transpose();
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
29fn main()
30{
31 let args: Vec<String> = env::args().collect();
32 let n: usize = match args.get(1) {
33 Some(s) => {
34 match s.parse::<usize>() {
35 Ok(tmp_n) => tmp_n,
36 Err(err) => {
37 eprintln!("{}", err);
38 exit(1);
39 },
40 }
41 },
42 None => 100,
43 };
44 let m: usize = match args.get(2) {
45 Some(s) => {
46 match s.parse::<usize>() {
47 Ok(tmp_m) => tmp_m,
48 Err(err) => {
49 eprintln!("{}", err);
50 exit(1);
51 },
52 }
53 },
54 None => 100,
55 };
56 let l: usize = match args.get(3) {
57 Some(s) => {
58 match s.parse::<usize>() {
59 Ok(tmp_l) => tmp_l,
60 Err(err) => {
61 eprintln!("{}", err);
62 exit(1);
63 },
64 }
65 },
66 None => 100,
67 };
68 let frontend = match Frontend::new() {
69 Ok(tmp_frontend) => tmp_frontend,
70 Err(err) => {
71 eprintln!("{}", err);
72 exit(1);
73 },
74 };
75 println!("backend: {}", frontend.backend().name());
76 let a = create_matrix(n, l, false);
77 let b = create_matrix(l, m, false);
78 let c = create_matrix(n, m, true);
79 let now = Instant::now();
80 let d = a * b + c;
81 let duration = now.elapsed();
82 let elems = d.elems();
83 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
84 println!("sum: {}", sum);
85 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
86 match finalize_default_backend() {
87 Ok(()) => (),
88 Err(err) => {
89 eprintln!("{}", err);
90 exit(1);
91 },
92 }
93}
Sourcepub fn copy(&self) -> Self
pub fn copy(&self) -> Self
Creates a matrix copy.
This method indeed copies the matrix array to a new matrix array.
Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Transposes the matrix ().
This method doesn’t indeed transpose the matrix but changes the transpose flag and exchanges the number of matrix rows with the number of matrix columns.
§Examples
let a = matrix![
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]
];
let b = a.transpose();
assert_eq!(3, b.row_count());
assert_eq!(2, b.col_count());
assert_eq!(true, b.is_transposed());
assert_eq!(a.elems(), b.elems());
let c = b.transpose();
assert_eq!(2, c.row_count());
assert_eq!(3, c.col_count());
assert_eq!(false, c.is_transposed());
assert_eq!(a.elems(), c.elems());
Examples found in repository?
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(m, l);
73 let now = Instant::now();
74 let c = a * b.transpose();
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
Sourcepub fn really_transpose(&self) -> Self
pub fn really_transpose(&self) -> Self
Indeed transposes the matrix ().
This method indeed transposes the matrix without changing the transpose flag.
§Examples
let a = matrix![
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]
];
let b = a.really_transpose();
assert_eq!(3, b.row_count());
assert_eq!(2, b.col_count());
assert_eq!(false, b.is_transposed());
assert_eq!(vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0], b.elems());
Sourcepub fn rt(&self) -> Self
pub fn rt(&self) -> Self
See really_transpose
.
Sourcepub fn mul_elems(&self, b: &Self) -> Self
pub fn mul_elems(&self, b: &Self) -> Self
Multiplies the matrix elements by the b
matrix elements
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = a.mul_elems(&b);
assert_eq!(vec![1.0 * 5.0, 2.0 * 6.0, 3.0 * 7.0, 4.0 * 8.0], c.elems());
Sourcepub fn div_elems(&self, b: &Self) -> Self
pub fn div_elems(&self, b: &Self) -> Self
Divides the matrix elements by the b
matrix elements
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = a.div_elems(&b);
let elems = c.elems();
assert!((1.0 / 5.0 - elems[0]).abs() < 0.001);
assert!((2.0 / 6.0 - elems[1]).abs() < 0.001);
assert!((3.0 / 7.0 - elems[2]).abs() < 0.001);
assert!((4.0 / 8.0 - elems[3]).abs() < 0.001);
Sourcepub fn rsub(&self, b: f32) -> Self
pub fn rsub(&self, b: f32) -> Self
Subtracts the matrix from the scalar ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.rsub(10.5);
assert_eq!(vec![10.5 - 1.0, 10.5 - 2.0, 10.5 - 3.0, 10.5 - 4.0], b.elems());
Sourcepub fn rdiv(&self, b: f32) -> Self
pub fn rdiv(&self, b: f32) -> Self
Divides the scalar by the matrix elements ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.rdiv(10.5);
let elems = b.elems();
assert!((10.5 / 1.0 - elems[0]).abs() < 0.001);
assert!((10.5 / 2.0 - elems[1]).abs() < 0.001);
assert!((10.5 / 3.0 - elems[2]).abs() < 0.001);
assert!((10.5 / 4.0 - elems[3]).abs() < 0.001);
Sourcepub fn sigmoid(&self) -> Self
pub fn sigmoid(&self) -> Self
Calculates sigmoid function for the matrix ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.sigmoid();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((1.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((1.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((1.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Sourcepub fn tanh(&self) -> Self
pub fn tanh(&self) -> Self
Calculates hiperbolic tangent function for the matrix ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.tanh();
let elems = b.elems();
assert!((1.0f32.tanh() - elems[0]).abs() < 0.001);
assert!((2.0f32.tanh() - elems[1]).abs() < 0.001);
assert!((3.0f32.tanh() - elems[2]).abs() < 0.001);
assert!((4.0f32.tanh() - elems[3]).abs() < 0.001);
Sourcepub fn swish(&self) -> Self
pub fn swish(&self) -> Self
Calculates swish function for the matrix ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.swish();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((2.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((3.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((4.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Sourcepub fn softmax(&self) -> Self
pub fn softmax(&self) -> Self
Calculates softmax function for the matrix ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.softmax();
let elems = b.elems();
let sum1 = 1.0f32.exp() + 3.0f32.exp();
let sum2 = 2.0f32.exp() + 4.0f32.exp();
assert!((1.0f32.exp() / sum1 - elems[0]).abs() < 0.001);
assert!((2.0f32.exp() / sum2 - elems[1]).abs() < 0.001);
assert!((3.0f32.exp() / sum1 - elems[2]).abs() < 0.001);
assert!((4.0f32.exp() / sum2 - elems[3]).abs() < 0.001);
Sourcepub fn sqrt(&self) -> Self
pub fn sqrt(&self) -> Self
Calculates square root of the matrix ().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = a.sqrt();
let elems = b.elems();
assert!((1.0f32.sqrt() - elems[0]).abs() < 0.001);
assert!((2.0f32.sqrt() - elems[1]).abs() < 0.001);
assert!((3.0f32.sqrt() - elems[2]).abs() < 0.001);
assert!((4.0f32.sqrt() - elems[3]).abs() < 0.001);
Sourcepub fn repeat(&self, n: usize) -> Self
pub fn repeat(&self, n: usize) -> Self
Repeats the vector as column or a row.
§Examples
let a = matrix![
[1.0],
[2.0]
];
let b = a.repeat(3);
assert_eq!(vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0], b.elems());
let c = matrix![[1.0, 2.0, 3.0]];
let d = c.repeat(2);
assert_eq!(vec![1.0, 2.0, 3.0, 1.0, 2.0, 3.0], d.elems());
Trait Implementations§
Source§impl AddAssign<&Matrix> for Matrix
impl AddAssign<&Matrix> for Matrix
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+=
operation. Read moreSource§impl AddAssign<&f32> for Matrix
impl AddAssign<&f32> for Matrix
Source§fn add_assign(&mut self, rhs: &f32)
fn add_assign(&mut self, rhs: &f32)
+=
operation. Read moreSource§impl AddAssign<f32> for Matrix
impl AddAssign<f32> for Matrix
Source§fn add_assign(&mut self, rhs: f32)
fn add_assign(&mut self, rhs: f32)
+=
operation. Read moreSource§impl AddAssign for Matrix
impl AddAssign for Matrix
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl DivAssign<&f32> for Matrix
impl DivAssign<&f32> for Matrix
Source§fn div_assign(&mut self, rhs: &f32)
fn div_assign(&mut self, rhs: &f32)
/=
operation. Read moreSource§impl DivAssign<f32> for Matrix
impl DivAssign<f32> for Matrix
Source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read moreSource§impl MulAssign<&Matrix> for Matrix
impl MulAssign<&Matrix> for Matrix
Source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
*=
operation. Read moreSource§impl MulAssign<&f32> for Matrix
impl MulAssign<&f32> for Matrix
Source§fn mul_assign(&mut self, rhs: &f32)
fn mul_assign(&mut self, rhs: &f32)
*=
operation. Read moreSource§impl MulAssign<f32> for Matrix
impl MulAssign<f32> for Matrix
Source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read moreSource§impl MulAssign for Matrix
impl MulAssign for Matrix
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moreSource§impl SubAssign<&Matrix> for Matrix
impl SubAssign<&Matrix> for Matrix
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
-=
operation. Read moreSource§impl SubAssign<&f32> for Matrix
impl SubAssign<&f32> for Matrix
Source§fn sub_assign(&mut self, rhs: &f32)
fn sub_assign(&mut self, rhs: &f32)
-=
operation. Read more