1 | //$$newmatrm.cpp rectangular matrix operations |
---|
2 | |
---|
3 | // Copyright (C) 1991,2,3,4: R B Davies |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | #include "newmat.h" |
---|
8 | #include "newmatrm.h" |
---|
9 | |
---|
10 | #ifdef use_namespace |
---|
11 | namespace NEWMAT { |
---|
12 | #endif |
---|
13 | |
---|
14 | #ifdef DO_REPORT |
---|
15 | #define REPORT { static ExeCounter ExeCount(__LINE__,12); ++ExeCount; } |
---|
16 | #else |
---|
17 | #define REPORT {} |
---|
18 | #endif |
---|
19 | |
---|
20 | |
---|
21 | // operations on rectangular matrices |
---|
22 | |
---|
23 | |
---|
24 | void RectMatrixRow::Reset (const Matrix& M, int row, int skip, int length) |
---|
25 | { |
---|
26 | REPORT |
---|
27 | RectMatrixRowCol::Reset |
---|
28 | ( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() ); |
---|
29 | } |
---|
30 | |
---|
31 | void RectMatrixRow::Reset (const Matrix& M, int row) |
---|
32 | { |
---|
33 | REPORT |
---|
34 | RectMatrixRowCol::Reset( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() ); |
---|
35 | } |
---|
36 | |
---|
37 | void RectMatrixCol::Reset (const Matrix& M, int skip, int col, int length) |
---|
38 | { |
---|
39 | REPORT |
---|
40 | RectMatrixRowCol::Reset |
---|
41 | ( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 ); |
---|
42 | } |
---|
43 | |
---|
44 | void RectMatrixCol::Reset (const Matrix& M, int col) |
---|
45 | { |
---|
46 | REPORT |
---|
47 | RectMatrixRowCol::Reset( M.Store()+col, M.Nrows(), M.Ncols(), 1 ); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | Real RectMatrixRowCol::SumSquare() const |
---|
52 | { |
---|
53 | REPORT |
---|
54 | long_Real sum = 0.0; int i = n; Real* s = store; int d = spacing; |
---|
55 | // while (i--) { sum += (long_Real)*s * *s; s += d; } |
---|
56 | if (i) for(;;) |
---|
57 | { sum += (long_Real)*s * *s; if (!(--i)) break; s += d; } |
---|
58 | return (Real)sum; |
---|
59 | } |
---|
60 | |
---|
61 | Real RectMatrixRowCol::operator*(const RectMatrixRowCol& rmrc) const |
---|
62 | { |
---|
63 | REPORT |
---|
64 | long_Real sum = 0.0; int i = n; |
---|
65 | Real* s = store; int d = spacing; |
---|
66 | Real* s1 = rmrc.store; int d1 = rmrc.spacing; |
---|
67 | if (i!=rmrc.n) |
---|
68 | { |
---|
69 | Tracer tr("newmatrm"); |
---|
70 | Throw(InternalException("Dimensions differ in *")); |
---|
71 | } |
---|
72 | // while (i--) { sum += (long_Real)*s * *s1; s += d; s1 += d1; } |
---|
73 | if (i) for(;;) |
---|
74 | { sum += (long_Real)*s * *s1; if (!(--i)) break; s += d; s1 += d1; } |
---|
75 | return (Real)sum; |
---|
76 | } |
---|
77 | |
---|
78 | void RectMatrixRowCol::AddScaled(const RectMatrixRowCol& rmrc, Real r) |
---|
79 | { |
---|
80 | REPORT |
---|
81 | int i = n; Real* s = store; int d = spacing; |
---|
82 | Real* s1 = rmrc.store; int d1 = rmrc.spacing; |
---|
83 | if (i!=rmrc.n) |
---|
84 | { |
---|
85 | Tracer tr("newmatrm"); |
---|
86 | Throw(InternalException("Dimensions differ in AddScaled")); |
---|
87 | } |
---|
88 | // while (i--) { *s += *s1 * r; s += d; s1 += d1; } |
---|
89 | if (i) for (;;) |
---|
90 | { *s += *s1 * r; if (!(--i)) break; s += d; s1 += d1; } |
---|
91 | } |
---|
92 | |
---|
93 | void RectMatrixRowCol::Divide(const RectMatrixRowCol& rmrc, Real r) |
---|
94 | { |
---|
95 | REPORT |
---|
96 | int i = n; Real* s = store; int d = spacing; |
---|
97 | Real* s1 = rmrc.store; int d1 = rmrc.spacing; |
---|
98 | if (i!=rmrc.n) |
---|
99 | { |
---|
100 | Tracer tr("newmatrm"); |
---|
101 | Throw(InternalException("Dimensions differ in Divide")); |
---|
102 | } |
---|
103 | // while (i--) { *s = *s1 / r; s += d; s1 += d1; } |
---|
104 | if (i) for (;;) { *s = *s1 / r; if (!(--i)) break; s += d; s1 += d1; } |
---|
105 | } |
---|
106 | |
---|
107 | void RectMatrixRowCol::Divide(Real r) |
---|
108 | { |
---|
109 | REPORT |
---|
110 | int i = n; Real* s = store; int d = spacing; |
---|
111 | // while (i--) { *s /= r; s += d; } |
---|
112 | if (i) for (;;) { *s /= r; if (!(--i)) break; s += d; } |
---|
113 | } |
---|
114 | |
---|
115 | void RectMatrixRowCol::Negate() |
---|
116 | { |
---|
117 | REPORT |
---|
118 | int i = n; Real* s = store; int d = spacing; |
---|
119 | // while (i--) { *s = - *s; s += d; } |
---|
120 | if (i) for (;;) { *s = - *s; if (!(--i)) break; s += d; } |
---|
121 | } |
---|
122 | |
---|
123 | void RectMatrixRowCol::Zero() |
---|
124 | { |
---|
125 | REPORT |
---|
126 | int i = n; Real* s = store; int d = spacing; |
---|
127 | // while (i--) { *s = 0.0; s += d; } |
---|
128 | if (i) for (;;) { *s = 0.0; if (!(--i)) break; s += d; } |
---|
129 | } |
---|
130 | |
---|
131 | void ComplexScale(RectMatrixCol& U, RectMatrixCol& V, Real x, Real y) |
---|
132 | { |
---|
133 | REPORT |
---|
134 | int n = U.n; |
---|
135 | if (n != V.n) |
---|
136 | { |
---|
137 | Tracer tr("newmatrm"); |
---|
138 | Throw(InternalException("Dimensions differ in ComplexScale")); |
---|
139 | } |
---|
140 | Real* u = U.store; Real* v = V.store; |
---|
141 | int su = U.spacing; int sv = V.spacing; |
---|
142 | //while (n--) |
---|
143 | //{ |
---|
144 | // Real z = *u * x - *v * y; *v = *u * y + *v * x; *u = z; |
---|
145 | // u += su; v += sv; |
---|
146 | //} |
---|
147 | if (n) for (;;) |
---|
148 | { |
---|
149 | Real z = *u * x - *v * y; *v = *u * y + *v * x; *u = z; |
---|
150 | if (!(--n)) break; |
---|
151 | u += su; v += sv; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | void Rotate(RectMatrixCol& U, RectMatrixCol& V, Real tau, Real s) |
---|
156 | { |
---|
157 | REPORT |
---|
158 | // (U, V) = (U, V) * (c, s) where tau = s/(1+c), c^2 + s^2 = 1 |
---|
159 | int n = U.n; |
---|
160 | if (n != V.n) |
---|
161 | { |
---|
162 | Tracer tr("newmatrm"); |
---|
163 | Throw(InternalException("Dimensions differ in Rotate")); |
---|
164 | } |
---|
165 | Real* u = U.store; Real* v = V.store; |
---|
166 | int su = U.spacing; int sv = V.spacing; |
---|
167 | //while (n--) |
---|
168 | //{ |
---|
169 | // Real zu = *u; Real zv = *v; |
---|
170 | // *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau); |
---|
171 | // u += su; v += sv; |
---|
172 | //} |
---|
173 | if (n) for(;;) |
---|
174 | { |
---|
175 | Real zu = *u; Real zv = *v; |
---|
176 | *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau); |
---|
177 | if (!(--n)) break; |
---|
178 | u += su; v += sv; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | #ifdef use_namespace |
---|
186 | } |
---|
187 | #endif |
---|
188 | |
---|
189 | |
---|