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
// Copyright © 2023 Marcel Luca Schmidt
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! Initialize a [`MatZ`] with common defaults, e.g., zero and identity.
use super::MatZ;
use crate::utils::index::evaluate_indices;
use flint_sys::fmpz_mat::{fmpz_mat_init, fmpz_mat_one};
use std::{fmt::Display, mem::MaybeUninit};
impl MatZ {
/// Creates a new matrix with `num_rows` rows, `num_cols` columns and
/// zeros as entries.
///
/// Parameters:
/// - `num_rows`: number of rows the new matrix should have
/// - `num_cols`: number of columns the new matrix should have
///
/// Returns a new [`MatZ`] instance of the provided dimensions.
///
/// # Examples
/// ```
/// use qfall_math::integer::MatZ;
///
/// let matrix = MatZ::new(5, 10);
/// ```
///
/// # Panics ...
/// - if the number of rows or columns is negative, `0`, or does not fit into an [`i64`].
pub fn new(
num_rows: impl TryInto<i64> + Display,
num_cols: impl TryInto<i64> + Display,
) -> Self {
let (num_rows_i64, num_cols_i64) = evaluate_indices(num_rows, num_cols).unwrap();
assert!(
num_rows_i64 != 0 && num_cols_i64 != 0,
"A matrix can not contain 0 rows or 0 columns"
);
let mut matrix = MaybeUninit::uninit();
unsafe {
fmpz_mat_init(matrix.as_mut_ptr(), num_rows_i64, num_cols_i64);
// Construct MatZ from previously initialized fmpz_mat
MatZ {
matrix: matrix.assume_init(),
}
}
}
/// Generate a `num_rows` times `num_columns` matrix with `1` on the
/// diagonal and `0` anywhere else.
///
/// Parameters:
/// - `rum_rows`: the number of rows of the identity matrix
/// - `num_columns`: the number of columns of the identity matrix
///
/// Returns a matrix with `1` across the diagonal and `0` anywhere else.
///
/// # Examples
/// ```
/// use qfall_math::integer::MatZ;
///
/// let matrix = MatZ::identity(2, 3);
///
/// let identity = MatZ::identity(10, 10);
/// ```
///
/// # Panics ...
/// - if the provided number of rows and columns are not suited to create a matrix.
/// For further information see [`MatZ::new`].
pub fn identity(
num_rows: impl TryInto<i64> + Display,
num_cols: impl TryInto<i64> + Display,
) -> Self {
let mut out = MatZ::new(num_rows, num_cols);
unsafe { fmpz_mat_one(&mut out.matrix) };
out
}
}
#[cfg(test)]
mod test_new {
use crate::{
integer::{MatZ, Z},
traits::MatrixGetEntry,
};
/// Ensure that entries of a new matrix are `0`.
#[test]
fn entry_zero() {
let matrix = MatZ::new(2, 2);
let entry_1 = matrix.get_entry(0, 0).unwrap();
let entry_2 = matrix.get_entry(0, 1).unwrap();
let entry_3 = matrix.get_entry(1, 0).unwrap();
let entry_4 = matrix.get_entry(1, 1).unwrap();
assert_eq!(Z::ZERO, entry_1);
assert_eq!(Z::ZERO, entry_2);
assert_eq!(Z::ZERO, entry_3);
assert_eq!(Z::ZERO, entry_4);
}
/// Ensure that a new zero matrix fails with `0` as `num_cols`.
#[should_panic]
#[test]
fn error_zero_num_cols() {
let _ = MatZ::new(1, 0);
}
/// Ensure that a new zero matrix fails with `0` as `num_rows`.
#[should_panic]
#[test]
fn error_zero_num_rows() {
let _ = MatZ::new(0, 1);
}
}
#[cfg(test)]
mod test_identity {
use crate::{
integer::{MatZ, Z},
traits::MatrixGetEntry,
};
/// Tests if an identity matrix is set from a zero matrix.
#[test]
fn identity() {
let matrix = MatZ::identity(10, 10);
for i in 0..10 {
for j in 0..10 {
if i != j {
assert_eq!(Z::ZERO, matrix.get_entry(i, j).unwrap());
} else {
assert_eq!(Z::ONE, matrix.get_entry(i, j).unwrap());
}
}
}
}
/// Tests if function works for a non-square matrix
#[test]
fn non_square_works() {
let matrix = MatZ::identity(10, 7);
for i in 0..10 {
for j in 0..7 {
if i != j {
assert_eq!(Z::ZERO, matrix.get_entry(i, j).unwrap());
} else {
assert_eq!(Z::ONE, matrix.get_entry(i, j).unwrap());
}
}
}
let matrix = MatZ::identity(7, 10);
for i in 0..7 {
for j in 0..10 {
if i != j {
assert_eq!(Z::ZERO, matrix.get_entry(i, j).unwrap());
} else {
assert_eq!(Z::ONE, matrix.get_entry(i, j).unwrap());
}
}
}
}
}