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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
struct SaveVars {
BOUND: f64,
FIRST: bool,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut BOUND: f64 = 0.0;
let mut FIRST: bool = false;
FIRST = true;
Self { BOUND, FIRST }
}
}
/// Invert nearly orthogonal matrices
///
/// Construct the inverse of a 3x3 matrix with orthogonal columns and
/// non-zero column norms using a numerically stable algorithm. The
/// rows of the output matrix are the columns of the input matrix
/// divided by the length squared of the corresponding columns.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// M I A 3x3 matrix.
/// MIT O M after transposition and scaling of rows.
/// ```
///
/// # Detailed Input
///
/// ```text
/// M is a 3x3 matrix.
/// ```
///
/// # Detailed Output
///
/// ```text
/// MIT is the matrix obtained by transposing M and dividing
/// the rows by squares of their norms.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If any of the columns of M have zero length, the error
/// SPICE(ZEROLENGTHCOLUMN) is signaled.
///
/// 2) If any column is too short to allow computation of the
/// reciprocal of its length without causing a floating
/// point overflow, the error SPICE(COLUMNTOOSMALL) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
/// Suppose that M is the matrix
///
/// .- -.
/// | A*u B*v C*w |
/// | 1 1 1 |
/// | |
/// | A*u B*v C*w |
/// | 2 2 2 |
/// | |
/// | A*u B*v C*w |
/// | 3 3 3 |
/// `- -'
///
/// where the vectors (u , u , u ), (v , v , v ), and (w , w , w )
/// 1 2 3 1 2 3 1 2 3
/// are unit vectors. This routine produces the matrix:
///
///
/// .- -.
/// | a*u a*u a*u |
/// | 1 2 3 |
/// | |
/// | b*v b*v b*v |
/// | 1 2 3 |
/// | |
/// | c*w c*w c*w |
/// | 1 2 3 |
/// `- -'
///
/// where a = 1/A, b = 1/B, and c = 1/C.
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as
/// input, the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
/// 1) Given a double precision 3x3 matrix with mutually orthogonal
/// rows of arbitrary length, compute its inverse. Check that the
/// original matrix times the computed inverse produces the
/// identity matrix.
///
/// Example code begins here.
///
///
/// PROGRAM INVORT_EX1
/// IMPLICIT NONE
///
/// C
/// C Local variables.
/// C
/// DOUBLE PRECISION IMAT ( 3, 3 )
/// DOUBLE PRECISION M ( 3, 3 )
/// DOUBLE PRECISION MOUT ( 3, 3 )
///
/// INTEGER I
/// INTEGER J
///
/// C
/// C Define a matrix to invert.
/// C
/// DATA M / 0.D0, 0.5D0, 0.D0,
/// . -1.D0, 0.D0, 0.D0,
/// . 0.D0, 0.D0, 1.D0 /
///
/// WRITE(*,*) 'Original Matrix:'
/// DO I=1, 3
///
/// WRITE(*,'(3F16.7)') ( M(I,J), J=1,3 )
///
/// END DO
/// C
/// C Invert the matrix, then output.
/// C
/// CALL INVORT ( M, MOUT )
///
/// WRITE(*,*) ' '
/// WRITE(*,*) 'Inverse Matrix:'
/// DO I=1, 3
///
/// WRITE(*,'(3F16.7)') ( MOUT(I,J), J=1,3 )
///
/// END DO
///
/// C
/// C Check the M times MOUT produces the identity matrix.
/// C
/// CALL MXM ( M, MOUT, IMAT )
///
/// WRITE(*,*) ' '
/// WRITE(*,*) 'Original times inverse:'
/// DO I=1, 3
///
/// WRITE(*,'(3F16.7)') ( IMAT(I,J), J=1,3 )
///
/// END DO
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Original Matrix:
/// 0.0000000 -1.0000000 0.0000000
/// 0.5000000 0.0000000 0.0000000
/// 0.0000000 0.0000000 1.0000000
///
/// Inverse Matrix:
/// 0.0000000 2.0000000 0.0000000
/// -1.0000000 0.0000000 0.0000000
/// 0.0000000 0.0000000 1.0000000
///
/// Original times inverse:
/// 1.0000000 0.0000000 0.0000000
/// 0.0000000 1.0000000 0.0000000
/// 0.0000000 0.0000000 1.0000000
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// E.D. Wright (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.2.0, 26-OCT-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard. Fixed I/O type
/// of argument MIT in $Brief_I/O table. Extended $Abstract
/// section.
///
/// Added complete code example to $Examples section.
///
/// - SPICELIB Version 1.1.1, 14-NOV-2013 (EDW)
///
/// Edit to $Abstract. Eliminated unneeded $Revisions section.
///
/// - SPICELIB Version 1.1.0, 02-SEP-2005 (NJB)
///
/// Updated to remove non-standard use of duplicate arguments
/// in VSCL call.
///
/// - SPICELIB Version 1.0.0, 02-JAN-2002 (WLT)
/// ```
pub fn invort(
ctx: &mut SpiceContext,
m: &[[f64; 3]; 3],
mit: &mut [[f64; 3]; 3],
) -> crate::Result<()> {
INVORT(m.as_flattened(), mit.as_flattened_mut(), ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure INVORT ( Invert nearly orthogonal matrices )
pub fn INVORT(M: &[f64], MIT: &mut [f64], ctx: &mut Context) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let M = DummyArray2D::new(M, 1..=3, 1..=3);
let mut MIT = DummyArrayMut2D::new(MIT, 1..=3, 1..=3);
let mut LENGTH: f64 = 0.0;
let mut SCALE: f64 = 0.0;
let mut TEMP = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
//
// SPICELIB functions
//
//
// Local Variables
//
//
// Saved variables
//
//
// Initial values
//
//
// Use discovery check-in.
//
//
// The first time through, get a copy of DPMAX.
//
if save.FIRST {
save.BOUND = DPMAX();
save.FIRST = false;
}
//
// For each column, construct a scaled copy. However, make sure
// everything is do-able before trying something.
//
for I in 1..=3 {
UNORM(M.subarray([1, I]), TEMP.subarray_mut([1, I]), &mut LENGTH);
if (LENGTH == 0.0) {
CHKIN(b"INVORT", ctx)?;
SETMSG(b"Column # of the input matrix has a norm of zero. ", ctx);
ERRINT(b"#", I, ctx);
SIGERR(b"SPICE(ZEROLENGTHCOLUMN)", ctx)?;
CHKOUT(b"INVORT", ctx)?;
return Ok(());
}
//
// Make sure we can actually rescale the rows.
//
if (LENGTH < 1.0) {
if ((LENGTH * save.BOUND) < 1.0) {
CHKIN(b"INVORT", ctx)?;
SETMSG(b"The length of column # is #. This number cannot be inverted. For this reason, the scaled transpose of the input matrix cannot be formed. ", ctx);
ERRINT(b"#", I, ctx);
ERRDP(b"#", LENGTH, ctx);
SIGERR(b"SPICE(COLUMNTOOSMALL)", ctx)?;
CHKOUT(b"INVORT", ctx)?;
return Ok(());
}
}
SCALE = (1.0 / LENGTH);
VSCLIP(SCALE, TEMP.subarray_mut([1, I]));
}
//
// If we make it this far, we just need to transpose TEMP into MIT.
//
XPOSE(TEMP.as_slice(), MIT.as_slice_mut());
Ok(())
}