-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_space.rs
More file actions
1038 lines (915 loc) · 31 KB
/
state_space.rs
File metadata and controls
1038 lines (915 loc) · 31 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
any::TypeId,
error::Error,
fmt,
marker::PhantomData,
ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
},
};
use crate::{Continuous, Discrete, utils::traits::Time};
extern crate nalgebra as na;
use na::DMatrix;
/// A state-space representation of a system.
///
/// # Members
///
/// - `a`: State Matrix
/// - `b`: Input Matrix
/// - `c`: Output Matrix
/// - `d`: Feedthrough Matrix
///
/// # Type Parameters
/// - `U`: The time domain. `Continuous` or `Discrete`
#[derive(Debug, Clone, PartialEq)]
pub struct Ss<U: Time> {
a: DMatrix<f64>,
b: DMatrix<f64>,
c: DMatrix<f64>,
d: DMatrix<f64>,
time: PhantomData<U>,
}
impl<U: Time + 'static> Ss<U> {
/// Creates a new state-space system from the given matrices.
///
/// # Parameters:
/// - `a`: The state matrix (A) must be square.
/// - `b`: The input matrix (B) must have rows equal to the number of
/// states.
/// - `c`: The output matrix (C) must have columns equal to the number of
/// states.
/// - `d`: The feedthrough matrix (D) must have rows equal to the number of
/// outputs and columns equal to the number of inputs.
///
/// # Returns:
/// - `Ok(Self)`: A state-space system on success.
/// - `Err`: An error if the matrix dimensions do not match the expected
/// sizes.
///
/// # Errors:
/// Returns an error if the dimensions of the matrices are inconsistent.
pub fn new(
a: DMatrix<f64>,
b: DMatrix<f64>,
c: DMatrix<f64>,
d: DMatrix<f64>,
) -> Result<Self, Box<dyn Error + 'static>> {
let ss = Self {
a,
b,
c,
d,
time: PhantomData::<U>,
};
ss.is_valid()?;
Ok(ss)
}
/// Returns a reference to the state matrix (A).
pub fn a(&self) -> &DMatrix<f64> {
self.assert_valid();
&self.a
}
/// Returns a reference to the state matrix (B).
pub fn b(&self) -> &DMatrix<f64> {
self.assert_valid();
&self.b
}
/// Returns a reference to the state matrix (C).
pub fn c(&self) -> &DMatrix<f64> {
self.assert_valid();
&self.c
}
/// Returns a reference to the state matrix (D).
pub fn d(&self) -> &DMatrix<f64> {
self.assert_valid();
&self.d
}
/// Returns the number of outputs in the system.
///
/// # Returns:
/// - The number of rows in the output matrix (C), which equals the number
/// of outputs.
///
/// # Panics:
/// Panics if the number of rows in C does not match the number of rows in
/// D.
pub fn noutputs(&self) -> usize {
self.assert_valid();
self.c.nrows()
}
/// Returns the number of inputs in the system.
///
/// # Returns:
/// - The number of columns in the input matrix (B), which equals the number
/// of inputs.
///
/// # Panics:
/// Panics if the number of columns in D does not match the number of
/// columns in B.
pub fn ninputs(&self) -> usize {
self.assert_valid();
self.b.ncols()
}
/// Returns the order of the system, which is the number of states.
///
/// # Returns:
/// - The number of rows (or columns) in the state matrix (A), representing
/// the system order.
///
/// # Panics:
/// Panics if the state matrix (A) is not square.
pub fn order(&self) -> usize {
self.assert_valid();
self.a.nrows()
}
/// Returns the shape of the system as a tuple (number of outputs, number of
/// inputs).
///
/// # Returns:
/// - A tuple `(ny, nu)` where `ny` is the number of outputs and `nu` is the
/// number of inputs.
pub fn shape(&self) -> (usize, usize) {
self.assert_valid();
(self.noutputs(), self.ninputs())
}
/// Verifies the consistency of the matrix dimensions for state-space
/// representation.
///
/// # Parameters:
/// - `a`: The state matrix (A), must be square.
/// - `b`: The input matrix (B), must have the same number of rows as A.
/// - `c`: The output matrix (C), must have the same number of columns as A.
/// - `d`: The feedthrough matrix (D), must have the same number of rows as
/// C and columns as B.
///
/// # Returns:
/// - `Ok(())`: If all matrix dimensions are consistent.
/// - `Err`: If any matrix dimensions are inconsistent, an error is returned
/// with a descriptive message.
pub fn verify_dimensions(
a: &DMatrix<f64>,
b: &DMatrix<f64>,
c: &DMatrix<f64>,
d: &DMatrix<f64>,
) -> Result<(), std::io::Error> {
if !a.is_square() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"A matrix must be square",
));
}
let nx = a.nrows();
if b.nrows() != nx {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Dimensions of A and B is not consisten",
));
}
let nu = b.ncols();
if c.ncols() != nx {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Dimensions of A and C is not consistent",
));
}
let ny = c.nrows();
if d.ncols() != nu || d.nrows() != ny {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Dimensions of B, C, D is not consistent",
));
}
Ok(())
}
fn is_valid(&self) -> Result<(), String> {
Ss::<U>::verify_dimensions(&self.a, &self.b, &self.c, &self.d)
.map_err(|e| e.to_string())
}
fn assert_valid(&self) {
let is_valid = self.is_valid();
assert!(
is_valid.is_ok(),
"Ss is invalid: Message: {}, Ss: {}",
is_valid.unwrap_err(),
self
);
}
/// Creates a new state-space system representing a static gain.
///
/// This function constructs a state-space system that consists only of a
/// gain matrix, without any internal states.
///
/// # Parameters:
/// - `gain`: The scalar gain value.
///
/// # Returns:
/// - A state-space system representing `y = gain * u`, where `y` is the
/// output and `u` is the input.
///
/// # Panics:
/// Panics if the system creation fails, which should not happen for valid
/// numerical inputs.
pub fn new_from_scalar(gain: f64) -> Self {
Self::new(
DMatrix::zeros(0, 0),
DMatrix::zeros(0, 1),
DMatrix::zeros(1, 0),
gain * DMatrix::identity(1, 1),
)
.unwrap()
}
/// Computes the inverse of a state-space system, if possible.
///
/// # Warning
/// The need for inverse often arrised due to feedback connections. Such as
/// T = L/(1 + L). For such cases use the `feedback` function, which is more
/// numerically stable.
///
/// # Errors
/// Returns an error if the direct transmission matrix (D) is not
/// invertible.
pub fn inv(&self) -> Result<Self, Box<dyn Error + 'static>> {
assert_eq!(
self.ninputs(),
self.noutputs(),
"System must be square to find the inverse"
);
self.assert_valid();
let d_inv = self
.d()
.clone()
.try_inverse()
.ok_or("Matrix D is not invertible")?;
let a_new = self.a() - self.b() * &d_inv * self.c();
let b_new = self.b() * &d_inv;
let c_new = -&d_inv * self.c();
let d_new = d_inv;
Ss::<U>::new(a_new, b_new, c_new, d_new)
}
/// Connects two systems in **series**.
///
/// Given two systems `self` and `sys2`, the output of `self` is fed as the
/// input to `sys2`.
///
/// Mathematically:
/// ```txt
/// u ---> [ self ] ---> [ sys2 ] ---> y
/// ```
///
/// # Returns
/// A new system representing the series connection of `self` and `sys2`.
pub fn series(&self, sys2: &Self) -> Self {
assert_eq!(
self.noutputs(),
sys2.ninputs(),
"Self output must be same as sys2 inputs"
);
self.assert_valid();
sys2.assert_valid();
let nx2 = sys2.order();
let nu2 = sys2.ninputs();
let ny2 = sys2.noutputs();
let nx1 = self.order();
let nu1 = self.ninputs();
let ny1 = self.noutputs();
assert_eq!(nu2, ny1);
let mut a_new = DMatrix::zeros(nx1 + nx2, nx1 + nx2);
a_new.view_mut((0, 0), (nx1, nx1)).copy_from(self.a());
a_new
.view_mut((nx1, 0), (nx2, nx1))
.copy_from(&(sys2.b() * self.c()));
a_new.view_mut((nx1, nx1), (nx2, nx2)).copy_from(sys2.a());
let mut b_new = DMatrix::zeros(nx1 + nx2, nu1);
b_new.view_mut((0, 0), (nx1, nu1)).copy_from(self.b());
b_new
.view_mut((nx1, 0), (nx2, nu1))
.copy_from(&(sys2.b() * self.d()));
let mut c_new = DMatrix::zeros(ny2, nx1 + nx2);
c_new
.view_mut((0, 0), (ny2, nx1))
.copy_from(&(sys2.d() * self.c()));
c_new.view_mut((0, nx1), (ny2, nx2)).copy_from(sys2.c());
let d_new = sys2.d() * self.d();
Ss::<U>::new(a_new, b_new, c_new, d_new).unwrap()
}
/// Connects two systems in **parallel**.
///
/// The two systems `self` and `sys2` operate independently on the same
/// input `u`, and their outputs are summed together.
///
/// Mathematically:
/// ```txt
/// |-----> [ self ] -----|
/// u --->| (sum)---> y
/// |-----> [ sys2 ] -----|
/// ```
///
/// # Returns
/// A new system representing the parallel connection of `self` and `sys2`.
pub fn parallel(&self, sys2: &Self) -> Self {
assert_eq!(
self.shape(),
sys2.shape(),
"System must have same shape to connnect in parallel"
);
self.assert_valid();
sys2.assert_valid();
let nu = self.ninputs();
let ny = self.noutputs();
let nx1 = self.order();
let nx2 = sys2.order();
let mut a_new = DMatrix::zeros(nx1 + nx2, nx1 + nx2);
a_new.view_mut((0, 0), (nx1, nx1)).copy_from(self.a());
a_new.view_mut((nx1, nx1), (nx2, nx2)).copy_from(sys2.a());
let mut b_new = DMatrix::zeros(nx1 + nx2, nu);
b_new.view_mut((0, 0), (nx1, nu)).copy_from(self.b());
b_new.view_mut((nx1, 0), (nx2, nu)).copy_from(sys2.b());
let mut c_new = DMatrix::zeros(ny, nx1 + nx2);
c_new.view_mut((0, 0), (ny, nx1)).copy_from(self.c());
c_new.view_mut((0, nx1), (ny, nx2)).copy_from(sys2.c());
let d_new = self.d() + sys2.d();
Ss::<U>::new(a_new, b_new, c_new, d_new).unwrap()
}
/// **Negative** Feedback connection of `self` with `sys2`.
///
/// The system `sys2` provides feedback to `self`, forming a closed-loop
/// system.
///
/// ## Diagram:
/// ```txt
/// -------- y
/// u ---->O--->| self |--------->
/// -1^ -------- |
/// | |
/// | -------- |
/// -----| sys2 |<----
/// --------
/// ```
///
/// # Assumptions
/// - The number of inputs of `self` must match the number of outputs of
/// `sys2`.
/// - The number of outputs of `self` must match the number of inputs of
/// `sys2`.
///
/// # Returns
/// A new system representing the negative feedback connection of `self`
/// with `sys2`.
///
/// # Panics
/// This function will panic if the input-output dimensions of `self` and
/// `sys2` do not match.
pub fn feedback(self, sys2: &Self) -> Self {
assert_eq!(self.ninputs(), sys2.noutputs());
assert_eq!(self.noutputs(), sys2.ninputs());
self.assert_valid();
sys2.assert_valid();
let i_d1d2 = DMatrix::identity(self.noutputs(), self.noutputs())
+ self.d() * sys2.d();
let i_d1d2_inv = i_d1d2.try_inverse().unwrap();
let mut c_new =
DMatrix::zeros(self.noutputs(), self.order() + sys2.order());
c_new
.view_mut((0, 0), (self.noutputs(), self.order()))
.copy_from(self.c());
c_new
.view_mut((0, self.order()), (self.noutputs(), sys2.order()))
.copy_from(&(-self.d() * sys2.c()));
c_new = i_d1d2_inv.clone() * c_new;
let d_new = i_d1d2_inv * self.d();
let mut a1_new =
DMatrix::zeros(self.order(), self.order() + sys2.order());
a1_new
.view_mut((0, 0), (self.order(), self.order()))
.copy_from(self.a());
a1_new
.view_mut((0, self.order()), (self.order(), sys2.order()))
.copy_from(&(-self.b() * sys2.c()));
a1_new -= self.b() * sys2.d() * &c_new;
let mut a2_new =
DMatrix::zeros(sys2.order(), self.order() + sys2.order());
a2_new
.view_mut((0, self.order()), (sys2.order(), sys2.order()))
.copy_from(sys2.a());
a2_new += sys2.b() * &c_new;
let mut a_new = DMatrix::zeros(
self.order() + sys2.order(),
self.order() + sys2.order(),
);
a_new
.view_mut((0, 0), (self.order(), self.order() + sys2.order()))
.copy_from(&a1_new);
a_new
.view_mut(
(self.order(), 0),
(sys2.order(), self.order() + sys2.order()),
)
.copy_from(&a2_new);
let mut b_new =
DMatrix::zeros(self.order() + sys2.order(), self.ninputs());
b_new
.view_mut((0, 0), (self.order(), self.ninputs()))
.copy_from(&(self.b() - self.b() * sys2.d() * &d_new));
b_new
.view_mut((self.order(), 0), (sys2.order(), self.ninputs()))
.copy_from(&(sys2.b() * &d_new));
Ss::<U>::new(a_new, b_new, c_new, d_new).unwrap()
}
}
impl<'b, U: Time + 'static> Add<&'b Ss<U>> for &Ss<U> {
type Output = Ss<U>;
/// Adds two state-space systems in parallel.
///
/// y = y1 + y2
///
/// # Panics
/// Panics if the input-output dimensions of both systems do not match.
fn add(self, rhs: &'b Ss<U>) -> Self::Output {
assert_eq!(self.shape(), rhs.shape());
self.assert_valid();
rhs.assert_valid();
self.parallel(rhs)
}
}
impl<U: Time + 'static> Add for Ss<U> {
type Output = Ss<U>;
/// Adds two state-space systems in parallel.
///
/// y = y1 + y2
///
/// # Panics
/// Panics if the input-output dimensions of both systems do not match.
fn add(self, rhs: Self) -> Self::Output {
&self + &rhs
}
}
impl<U: Time + 'static> Neg for Ss<U> {
type Output = Ss<U>;
/// Negates the output of a state-space system.
///
/// I.e. y_new = -y
fn neg(mut self) -> Self::Output {
self.c *= -1.0;
self.d *= -1.0;
self.assert_valid();
self
}
}
impl<'b, U: Time + 'static> Sub<&'b Ss<U>> for &Ss<U> {
type Output = Ss<U>;
/// Subtracts two state-space systems by adding one and negating the other.
fn sub(self, rhs: &'b Ss<U>) -> Self::Output {
self.assert_valid();
rhs.assert_valid();
// TODO: should not need to use clone
self + &(-rhs.clone())
}
}
impl<U: Time + 'static> Sub for Ss<U> {
type Output = Ss<U>;
/// Subtracts two state-space systems by adding one and negating the other.
fn sub(self, rhs: Self) -> Self::Output {
self.assert_valid();
rhs.assert_valid();
self + (-rhs)
}
}
impl<'b, U: Time + 'static> Mul<&'b Ss<U>> for &Ss<U> {
type Output = Ss<U>;
/// Multiplies two state-space systems in series (cascading connection).
///
/// The resulting system represents `y <- self <- rhs <- u`, where `self`
/// follows `rhs`.
///
/// # Panics
/// Panics if the output size of `rhs` does not match the input size of
/// `self`.
fn mul(self, rhs: &'b Ss<U>) -> Self::Output {
self.assert_valid();
rhs.assert_valid();
rhs.series(self)
}
}
impl<U: Time + 'static> Mul for Ss<U> {
type Output = Ss<U>;
/// Multiplies two state-space systems in series (cascading connection).
///
/// The resulting system represents `y <- self <- rhs <- u`, where `self`
/// follows `rhs`.
///
/// # Panics
/// Panics if the output size of `rhs` does not match the input size of
/// `self`.
fn mul(self, rhs: Self) -> Self::Output {
&self * &rhs
}
}
impl<'b, U: Time + 'static> Div<&'b Ss<U>> for &Ss<U> {
type Output = Ss<U>;
/// Divides one state-space system by another (right multiplication by
/// inverse).
///
/// # Warning
/// The need for division often arrised due to feedback connections. Such as
/// T = L/(1 + L). For such cases use the `feedback` function, which is more
/// numerically stable.
///
/// # Panics
/// Panics if the inverse of `rhs` cannot be computed.
#[allow(clippy::suspicious_arithmetic_impl)]
fn div(self, rhs: &'b Ss<U>) -> Self::Output {
self.assert_valid();
rhs.assert_valid();
self * &rhs.inv().unwrap()
}
}
impl<U: Time + 'static> Div for Ss<U> {
type Output = Ss<U>;
/// Divides one state-space system by another (right multiplication by
/// inverse).
///
/// # Warning
/// The need for division often arrised due to feedback connections. Such as
/// T = L/(1 + L). For such cases use the `feedback` function, which is more
/// numerically stable.
///
/// # Panics
/// Panics if the inverse of `rhs` cannot be computed.
#[allow(clippy::suspicious_arithmetic_impl)]
fn div(self, rhs: Self) -> Self::Output {
&self / &rhs
}
}
/// Implements compound assignment operators (`+=`, `-=`, `*=`, `/=`) for
/// state-space systems.
macro_rules! impl_compound_assign {
($struct_type:ident, [$(($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident )), *]) => {
$(
impl<U: Time + 'static> $assign_trait for $struct_type<U>
{
fn $assign_method(&mut self, rhs: Self) {
*self = self.clone().$method(rhs)
}
}
impl<'a, U: Time + 'static> $assign_trait<&'a $struct_type<U>> for $struct_type<U>
{
fn $assign_method(&mut self, rhs: &'a $struct_type<U>) {
*self = <&Self as $trait<&Self>>::$method(self, rhs);
}
}
)*
};
}
impl_compound_assign!(
Ss,
[
(Add, add, AddAssign, add_assign),
(Sub, sub, SubAssign, sub_assign),
(Mul, mul, MulAssign, mul_assign),
(Div, div, DivAssign, div_assign)
]
);
/// Implements scalar multiplication and division for state-space systems.
macro_rules! impl_scalar_math_operator_ss {
([$(($operator:ident, $operator_fn:ident)), *]) => {
$(
impl<U: Time + 'static> $operator<f64> for Ss<U> {
type Output = Self;
fn $operator_fn(self, rhs: f64) -> Self::Output {
let rhs_ss = Ss::<U>::new_from_scalar(rhs);
self.$operator_fn(rhs_ss)
}
}
impl<U: Time + 'static> $operator<Ss<U>> for f64 {
type Output = Ss<U>;
fn $operator_fn(self, rhs: Ss<U>) -> Self::Output {
let lhs_ss = Ss::<U>::new_from_scalar(self);
lhs_ss.$operator_fn(rhs)
}
}
impl<U: Time + 'static> $operator<f64> for &Ss<U> {
type Output = Ss<U>;
fn $operator_fn(self, rhs: f64) -> Self::Output {
let rhs_ss = Ss::<U>::new_from_scalar(rhs);
self.$operator_fn(&rhs_ss)
}
}
impl<U: Time + 'static> $operator<&Ss<U>> for f64 {
type Output = Ss<U>;
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
let scalar_ss = Ss::<U>::new_from_scalar(self);
(&scalar_ss).$operator_fn(rhs)
}
}
)*
};
}
impl_scalar_math_operator_ss!([(Add, add), (Sub, sub), (Mul, mul), (Div, div)]);
macro_rules! impl_comb_ref_and_no_ref_operators {
([$(($operator:ident, $operator_fn:ident)), *]) => {
$(
impl<U: Time + 'static> $operator<&Ss<U>> for Ss<U>
{
type Output = Ss<U>;
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
(&self).$operator_fn(rhs)
}
}
impl<U: Time + 'static> $operator<Ss<U>> for &Ss<U>
{
type Output = Ss<U>;
fn $operator_fn(self, rhs: Ss<U>) -> Self::Output {
self.$operator_fn(&rhs)
}
}
)*
};
}
impl_comb_ref_and_no_ref_operators!([
(Add, add),
(Sub, sub),
(Mul, mul),
(Div, div)
]);
impl<U: Time + 'static> fmt::Display for Ss<U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"\nA = {} B = {} C = {} D = {}",
self.a(),
self.b(),
self.c(),
self.d()
)?;
let time_domain = if TypeId::of::<U>() == TypeId::of::<Continuous>() {
"Continuous"
} else if TypeId::of::<U>() == TypeId::of::<Discrete>() {
"Discrete"
} else {
"Unknown"
};
write!(f, "{time_domain}-time state-space model\n\n")
}
}
#[cfg(test)]
mod tests {
use core::f64;
use approx::assert_abs_diff_eq;
use num_complex::c64;
use rand::Rng;
use super::*;
use crate::{
FrequencyResponse,
analysis::frequency_response::lin_space,
systems::Tf,
transformations::SsRealization::{ControllableCF, ObservableCF},
utils::traits::Continuous,
};
fn rand_proper_tf<U: Rng>(
rng: &mut U,
max_order: usize,
) -> Tf<f64, Continuous> {
let den_order = rng.random_range(1..=max_order);
let num_order = rng.random_range(0..=den_order);
let num: Vec<f64> = (0..=num_order)
.map(|_| rng.random_range(-10.0..10.0))
.collect();
let mut den: Vec<f64> = (0..den_order)
.map(|_| rng.random_range(-10.0..10.0))
.collect();
let mut den_max = rng.random_range(0.5..10.0); // ensure not too close to zero (possible division by close to zero)
if rng.random_range(0..=1) != 1 {
den_max *= -1.0; // negative for odd i
}
den.push(den_max);
Tf::<f64, Continuous>::new(&num, &den)
}
#[test]
fn new_ss() {
let a = DMatrix::from_row_slice(2, 2, &[1., 0., 0., 1.]);
let b = DMatrix::from_row_slice(2, 1, &[1., 1.]);
let c = DMatrix::from_row_slice(1, 2, &[1., 0.]);
let d = DMatrix::zeros(1, 1);
assert!(
Ss::<Continuous>::new(a.clone(), b.clone(), c.clone(), d.clone())
.is_ok()
);
let a_wrong = DMatrix::zeros(1, 2);
assert!(
Ss::<Continuous>::new(
a_wrong.clone(),
b.clone(),
c.clone(),
d.clone()
)
.is_err()
);
let b_wrong = DMatrix::zeros(2, 2);
assert!(
Ss::<Continuous>::new(
a.clone(),
b_wrong.clone(),
c.clone(),
d.clone()
)
.is_err()
);
let c_wrong = DMatrix::zeros(2, 1);
assert!(
Ss::<Continuous>::new(
a.clone(),
b.clone(),
c_wrong.clone(),
d.clone()
)
.is_err()
);
let d_wrong = DMatrix::zeros(1, 2);
assert!(
Ss::<Continuous>::new(
a.clone(),
b.clone(),
c.clone(),
d_wrong.clone()
)
.is_err()
);
}
#[test]
fn ss_arithmetic() {
let mut any_div_test = false;
for _ in 0..1000 {
let mut rng = rand::rng();
let tf1 = rand_proper_tf(&mut rng, 5);
let tf2 = rand_proper_tf(&mut rng, 5);
let ss1 = tf1.to_ss_method(ControllableCF).unwrap();
let ss2 = tf2.to_ss_method(ObservableCF).unwrap();
let tf_add = tf1.clone() + tf2.clone();
let tf_add = tf_add.to_ss().unwrap().to_tf().unwrap();
let ss_add = ss1.clone() + ss2.clone();
assert_abs_diff_eq!(
tf_add,
ss_add.to_tf().unwrap(),
epsilon = 1e-2
);
let tf_sub = tf1.clone() - tf2.clone();
let tf_sub = tf_sub.to_ss().unwrap().to_tf().unwrap();
let ss_sub = ss1.clone() - ss2.clone();
let ss_sub_tf = ss_sub.to_tf().unwrap();
assert_abs_diff_eq!(tf_sub, ss_sub_tf, epsilon = 1e-2);
let tf_mul = tf1.clone() * tf2.clone();
let tf_mul = tf_mul.to_ss().unwrap().to_tf().unwrap();
let ss_mul = ss1.clone() * ss2.clone();
assert_abs_diff_eq!(
tf_mul,
ss_mul.to_tf().unwrap(),
epsilon = 1e-2
);
if !tf2.is_strictly_proper() {
any_div_test = true;
let tf_div = tf1.clone() / tf2.clone();
let tf_div = tf_div.to_ss().unwrap().to_tf().unwrap();
let ss_div = ss1.clone() / ss2.clone();
assert_abs_diff_eq!(
tf_div,
ss_div.to_tf().unwrap(),
epsilon = 1e-2
);
}
}
assert!(any_div_test);
}
#[test]
fn ss_compund_assign() {
let ss1 = (1.0 / Tf::s()).to_ss().unwrap();
let ss2 = (Tf::s() / (1.0 + Tf::s())).to_ss().unwrap();
let ss1_sum = ss1.clone() + ss2.clone();
let mut ss1_sum_c = ss1.clone();
ss1_sum_c += ss2.clone();
assert_eq!(ss1_sum, ss1_sum_c);
let ss1_sub = ss1.clone() - ss2.clone();
let mut ss1_sub_c = ss1.clone();
ss1_sub_c -= ss2.clone();
assert_eq!(ss1_sub, ss1_sub_c);
let ss1_mul = ss1.clone() * ss2.clone();
let mut ss1_mul_c = ss1.clone();
ss1_mul_c *= ss2.clone();
assert_eq!(ss1_mul, ss1_mul_c);
let ss1_div = ss1.clone() / ss2.clone();
let mut ss1_div_c = ss1.clone();
ss1_div_c /= ss2.clone();
assert_eq!(ss1_div, ss1_div_c);
}
#[test]
fn ss_scalar_arithmetic() {
for scalar in lin_space(-10., 10., 1000) {
if scalar.abs() < 0.1 {
continue; // avoid division by zero
}
let tf_start = Tf::s() / (Tf::s() + 1.);
let ss_start = tf_start.to_ss().unwrap();
let ss_add = scalar + ss_start.clone();
assert_abs_diff_eq!(
ss_add.to_tf().unwrap(),
scalar + tf_start.clone()
);
let ss_add = ss_start.clone() + scalar;
assert_abs_diff_eq!(
ss_add.to_tf().unwrap(),
scalar + tf_start.clone()
);
let ss_sub = scalar - ss_start.clone();
assert_abs_diff_eq!(
ss_sub.to_tf().unwrap(),
scalar - tf_start.clone()
);
let ss_sub = ss_start.clone() - scalar;
assert_abs_diff_eq!(
ss_sub.to_tf().unwrap(),
tf_start.clone() - scalar
);
let ss_mul = scalar * ss_start.clone();
assert_abs_diff_eq!(
ss_mul.to_tf().unwrap(),
scalar * tf_start.clone()
);
let ss_mul = ss_start.clone() * scalar;
assert_abs_diff_eq!(
ss_mul.to_tf().unwrap(),
scalar * tf_start.clone()
);
let ss_div = scalar / ss_start.clone();
assert_abs_diff_eq!(
ss_div.to_tf().unwrap(),
scalar / tf_start.clone()
);
let ss_div = ss_start.clone() / scalar;
assert_abs_diff_eq!(
ss_div.to_tf().unwrap(),
tf_start.clone() / scalar
);
}
}
#[test]
fn ss_connections() {
let ss1 = (Tf::s() / (Tf::s() + 1.0)).to_ss().unwrap();
let ss2 = (1.0 / Tf::s()).to_ss().unwrap();
let ss_fb = ss1.clone().feedback(&ss2);
let tf_fb = ss_fb.to_tf().unwrap();
assert_abs_diff_eq!(tf_fb, Tf::s() / (Tf::s() + 2.0));
let ss2 = Ss::<Continuous>::new_from_scalar(1.0);
let ss_fb = ss1.clone().feedback(&ss2);
let tf_fb = ss_fb.to_tf().unwrap();
assert_abs_diff_eq!(tf_fb, 0.5 * Tf::s() / (Tf::s() + 0.5));
let ss_add = ss1.clone() + ss2.clone();
let ss_parallel = ss1.parallel(&ss2);
assert_eq!(ss_add, ss_parallel);
let ss_mul = ss2.clone() * ss1.clone();
let ss_series = ss1.series(&ss2);
assert_eq!(ss_mul, ss_series);
}
#[test]
fn ss_and_tf_feedback_match() {
for _ in 0..1000 {
let mut rng = rand::rng();
let tf1 = rand_proper_tf(&mut rng, 5);
let tf2 = rand_proper_tf(&mut rng, 5);
let ss1 = tf1.to_ss_method(ControllableCF).unwrap();
let ss2 = tf2.to_ss_method(ObservableCF).unwrap();
let ss_fb = ss1.feedback(&ss2);
let ss_fb = ss_fb.to_tf().unwrap();
let tf_fb = tf1.feedback(&tf2);
assert_abs_diff_eq!(tf_fb, ss_fb, epsilon = 1e-1);
}
}
#[test]