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
use crate::StrError;
use crate::{ComplexCooMatrix, CooMatrix};
use russell_lab::cpx;
impl ComplexCooMatrix {
/// Assigns this matrix to the values of another real matrix (scaled)
///
/// Performs:
///
/// ```text
/// this = (α + βi) · other
/// ```
///
/// Thus:
///
/// ```text
/// this[p].real = α · other[p]
/// this[p].imag = β · other[p]
///
/// other[p] ∈ Reals
/// p = [0, nnz(other)]
/// ```
///
/// **Warning:** make sure to allocate `max_nnz ≥ nnz(other)`.
///
/// # Examples
///
/// ```
/// use russell_lab::cpx;
/// use russell_sparse::prelude::*;
/// use russell_sparse::StrError;
///
/// fn main() -> Result<(), StrError> {
/// let mut b = CooMatrix::new(2, 2, 3, Sym::No)?;
/// b.put(0, 0, 2.0)?;
/// b.put(0, 1, 4.0)?;
/// b.put(1, 1, 6.0)?;
///
/// let mut a = ComplexCooMatrix::new(2, 2, 3, Sym::No)?;
/// a.assign_real(1.0, 2.0, &b)?;
///
/// // a now equals (1+2i) * b
/// assert_eq!(
/// format!("{}", a.as_dense()),
/// "┌ ┐\n\
/// │ 2+4i 4+8i │\n\
/// │ 0+0i 6+12i │\n\
/// └ ┘"
/// );
/// Ok(())
/// }
/// ```
pub fn assign_real(&mut self, alpha: f64, beta: f64, other: &CooMatrix) -> Result<(), StrError> {
if other.nrow != self.nrow {
return Err("matrices must have the same nrow");
}
if other.ncol != self.ncol {
return Err("matrices must have the same ncol");
}
if other.symmetric != self.symmetric {
return Err("matrices must have the same symmetric flag");
}
self.reset();
for p in 0..other.nnz {
let i = other.indices_i[p] as usize;
let j = other.indices_j[p] as usize;
self.put(i, j, cpx!(alpha * other.values[p], beta * other.values[p]))?;
}
Ok(())
}
/// Puts the entries of another real matrix into this matrix
///
/// Effectively, performs:
///
/// ```text
/// this += (α + βi) · other
/// ```
///
/// Thus:
///
/// ```text
/// this[p].real += α · other[p]
/// this[p].imag += β · other[p]
///
/// other[p] ∈ Reals
/// p = [0, nnz(other)]
/// ```
///
/// # Arguments
///
/// * `alpha` -- scaling factor
/// * `other` -- the other matrix to be added. It must be at most as large as `this`.
///
/// # Requirements
///
/// * `other.nrow ≤ this.nrow`
/// * `other.ncol ≤ this.ncol`
/// * `other.symmetric == this.symmetric`
///
/// # Note
///
/// * make sure to allocate `max_nnz ≥ nnz(this) + nnz(other)`.
///
/// # Examples
///
/// ```
/// use russell_lab::cpx;
/// use russell_sparse::prelude::*;
/// use russell_sparse::StrError;
///
/// fn main() -> Result<(), StrError> {
/// let mut a = ComplexCooMatrix::new(2, 2, 3, Sym::No)?;
/// a.put(0, 0, cpx!(10.0, 20.0))?;
/// a.put(1, 1, cpx!(30.0, 40.0))?;
///
/// let mut b = CooMatrix::new(2, 2, 1, Sym::No)?;
/// b.put(0, 0, 1.0)?;
///
/// a.add_real(2.0, 3.0, &b)?;
///
/// // (10+20i) + (2+3i)*(1) = 12+23i
/// assert_eq!(
/// format!("{}", a.as_dense()),
/// "┌ ┐\n\
/// │ 12+23i 0+0i │\n\
/// │ 0+0i 30+40i │\n\
/// └ ┘"
/// );
/// Ok(())
/// }
/// ```
pub fn add_real(&mut self, alpha: f64, beta: f64, other: &CooMatrix) -> Result<(), StrError> {
if other.nrow > self.nrow {
return Err("other.nrow must be ≤ this.nrow");
}
if other.ncol > self.ncol {
return Err("other.ncol must be ≤ this.ncol");
}
if other.symmetric != self.symmetric {
return Err("matrices must have the same symmetric flag");
}
for p in 0..other.nnz {
let i = other.indices_i[p] as usize;
let j = other.indices_j[p] as usize;
self.put(i, j, cpx!(alpha * other.values[p], beta * other.values[p]))?;
}
Ok(())
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use crate::{ComplexCooMatrix, CooMatrix, Sym};
use russell_lab::cpx;
#[test]
fn assign_real_capture_errors() {
let nnz_a = 1;
let nnz_b = 2; // wrong: must be ≤ nnz_a
let mut a_1x2 = ComplexCooMatrix::new(1, 2, nnz_a, Sym::No).unwrap();
let b_2x1 = CooMatrix::new(2, 1, nnz_b, Sym::No).unwrap();
let b_1x3 = CooMatrix::new(1, 3, nnz_b, Sym::No).unwrap();
let mut b_1x2 = CooMatrix::new(1, 2, nnz_b, Sym::No).unwrap();
a_1x2.put(0, 0, cpx!(123.0, 321.0)).unwrap();
b_1x2.put(0, 0, 456.0).unwrap();
b_1x2.put(0, 1, 654.0).unwrap();
assert_eq!(
a_1x2.assign_real(2.0, 3.0, &b_2x1).err(),
Some("matrices must have the same nrow")
);
assert_eq!(
a_1x2.assign_real(2.0, 3.0, &b_1x3).err(),
Some("matrices must have the same ncol")
);
assert_eq!(
a_1x2.assign_real(2.0, 3.0, &b_1x2).err(),
Some("COO matrix: max number of items has been reached")
);
let mut a_2x2 = ComplexCooMatrix::new(2, 2, 1, Sym::YesLower).unwrap();
let b_2x2 = CooMatrix::new(2, 2, 1, Sym::YesFull).unwrap();
assert_eq!(
a_2x2.assign_real(2.0, 3.0, &b_2x2).err(),
Some("matrices must have the same symmetric flag")
);
}
#[test]
fn assign_real_works() {
let nnz = 2;
let mut a = ComplexCooMatrix::new(3, 2, nnz, Sym::No).unwrap();
let mut b = CooMatrix::new(3, 2, nnz, Sym::No).unwrap();
a.put(2, 1, cpx!(1000.0, 2000.0)).unwrap();
b.put(0, 0, 10.0).unwrap();
b.put(2, 1, 20.0).unwrap();
assert_eq!(
format!("{}", a.as_dense()),
"┌ ┐\n\
│ 0+0i 0+0i │\n\
│ 0+0i 0+0i │\n\
│ 0+0i 1000+2000i │\n\
└ ┘"
);
a.assign_real(3.0, 2.0, &b).unwrap();
assert_eq!(
format!("{}", a.as_dense()),
"┌ ┐\n\
│ 30+20i 0+0i │\n\
│ 0+0i 0+0i │\n\
│ 0+0i 60+40i │\n\
└ ┘"
);
}
#[test]
fn add_real_capture_errors() {
let nnz_a = 1;
let nnz_b = 1;
let mut a_1x2 = ComplexCooMatrix::new(1, 2, nnz_a /* + nnz_b */, Sym::No).unwrap();
let b_2x1 = CooMatrix::new(2, 1, nnz_b, Sym::No).unwrap();
let b_1x3 = CooMatrix::new(1, 3, nnz_b, Sym::No).unwrap();
let mut b_1x2 = CooMatrix::new(1, 2, nnz_b, Sym::No).unwrap();
a_1x2.put(0, 0, cpx!(123.0, 321.0)).unwrap();
b_1x2.put(0, 0, 456.0).unwrap();
assert_eq!(
a_1x2.add_real(2.0, 3.0, &b_2x1).err(),
Some("other.nrow must be ≤ this.nrow")
);
assert_eq!(
a_1x2.add_real(2.0, 3.0, &b_1x3).err(),
Some("other.ncol must be ≤ this.ncol")
);
assert_eq!(
a_1x2.add_real(2.0, 3.0, &b_1x2).err(),
Some("COO matrix: max number of items has been reached")
);
let mut a_2x2 = ComplexCooMatrix::new(2, 2, 1, Sym::YesLower).unwrap();
let b_2x2 = CooMatrix::new(2, 2, 1, Sym::YesFull).unwrap();
assert_eq!(
a_2x2.add_real(2.0, 3.0, &b_2x2).err(),
Some("matrices must have the same symmetric flag")
);
}
#[test]
fn add_real_works() {
let nnz_a = 1;
let nnz_b = 2;
let mut a = ComplexCooMatrix::new(3, 2, nnz_a + nnz_b, Sym::No).unwrap();
let mut b = CooMatrix::new(3, 2, nnz_b, Sym::No).unwrap();
a.put(2, 1, cpx!(1000.0, 2000.0)).unwrap();
b.put(0, 0, 10.0).unwrap();
b.put(2, 1, 20.0).unwrap();
assert_eq!(
format!("{}", a.as_dense()),
"┌ ┐\n\
│ 0+0i 0+0i │\n\
│ 0+0i 0+0i │\n\
│ 0+0i 1000+2000i │\n\
└ ┘"
);
a.add_real(3.0, 2.0, &b).unwrap();
assert_eq!(
format!("{}", a.as_dense()),
"┌ ┐\n\
│ 30+20i 0+0i │\n\
│ 0+0i 0+0i │\n\
│ 0+0i 1060+2040i │\n\
└ ┘"
);
}
}