rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
//
// GENERATED FILE
//

use super::*;
use f2rust_std::*;

const TIGHT: f64 = 0.00000000000001;
const VTIGHT: f64 = 0.000000000000001;
const SEED0: i32 = 19920810;
const N: i32 = 3;
const NE: i32 = (N * N);

//$Procedure F_MATRIX3 (Family of tests on 3x3-matrix operations)
pub fn F_MATRIX3(OK: &mut bool, ctx: &mut Context) -> f2rust_std::Result<()> {
    let mut INDX: i32 = 0;
    let mut SEED: i32 = 0;
    let mut VINDX = StackArray::<f64, 3>::new(1..=N);
    let mut VRAN = StackArray::<f64, 3>::new(1..=N);
    let mut VOUT = StackArray::<f64, 3>::new(1..=N);
    let mut VEXPT = StackArray::<f64, 3>::new(1..=N);
    let mut VTEMP = StackArray::<f64, 3>::new(1..=N);
    let mut MZERO = StackArray2D::<f64, 9>::new(1..=N, 1..=N);
    let mut MRAN = StackArray2D::<f64, 9>::new(1..=N, 1..=N);
    let mut MINDX = StackArray2D::<f64, 9>::new(1..=N, 1..=N);
    let mut MOUT = StackArray2D::<f64, 9>::new(1..=N, 1..=N);
    let mut EVAL: f64 = 0.0;
    let mut EXPT: f64 = 0.0;

    //
    // Test Utility Functions
    //

    //
    // SPICELIB Functions
    //

    //
    // Local Parameters
    //

    //
    // Initial seed number for pseudo-random number generation
    //

    //
    // Dimension of 3-by-3 square matrices
    //

    //
    // Number of elements in an N-by-N matrix.
    //

    //
    // Local Variables
    //

    //
    // indices
    //

    //
    // Seed number for pseudo-random number generation
    //

    //
    // 3-component vectors
    //

    //
    // 3-by-3 matrices
    //

    //
    // Evaluated number from a library routine.
    //

    //
    // Expected value of EVAL
    //

    //
    // Begin every test family with an open call.
    //
    testutil::TOPEN(b"F_MATRIX3", ctx)?;

    //
    // Form arrays to be used for more than one test case.
    //

    //
    // A zero matrix
    //
    for J in 1..=N {
        for I in 1..=N {
            MZERO[[I, J]] = 0.0;
        }
    }

    //
    // An index vector, whose components are their indices
    //
    for I in 1..=N {
        VINDX[I] = (I as f64);
    }

    //
    // An index matrix, whose elements are their column-wise
    // storage indices
    //
    INDX = 0;
    for J in 1..=N {
        for I in 1..=N {
            INDX = (INDX + 1);
            MINDX[[I, J]] = (INDX as f64);
        }
    }

    //
    // Initialize the seed number of random number generation.
    //
    SEED = SEED0;

    //
    // Form a random vector, whose components are
    // uniformly distributed on the interval [-1.0D0, +1.0D0].
    //

    for I in 1..=N {
        VRAN[I] = testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?;
    }

    //
    // Form a random matrix, whose elements are
    // uniformly distributed on the interval [-1.0D0, +1.0D0].
    //
    for J in 1..=N {
        for I in 1..=N {
            MRAN[[I, J]] = testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?;
        }
    }

    //
    // Cases to test MEQU
    //

    testutil::TCASE(b"MEQU: copying a 3x3 zero matrix and checking strict equality between the zero matrix and the copied matrix", ctx)?;

    spicelib::MEQU(MZERO.as_slice(), MOUT.as_slice_mut());

    testutil::CHCKAD(
        b"MOUT",
        MOUT.as_slice(),
        b"=",
        MZERO.as_slice(),
        NE,
        0.0,
        OK,
        ctx,
    )?;

    testutil::TCASE(b"MEQU: copying a 3x3 random matrix and checking strict equality between the random matrix and the copied matrix", ctx)?;

    spicelib::MEQU(MRAN.as_slice(), MOUT.as_slice_mut());

    testutil::CHCKAD(
        b"MOUT",
        MOUT.as_slice(),
        b"=",
        MRAN.as_slice(),
        NE,
        0.0,
        OK,
        ctx,
    )?;

    //
    // Cases to test XPOSE
    //
    testutil::TCASE(b"XPOSE: generating the transpose of an index matrix, and checking equality between original and transposed elements", ctx)?;

    spicelib::XPOSE(MINDX.as_slice(), MOUT.as_slice_mut());

    INDX = 0;
    for J in 1..=N {
        for I in 1..=N {
            INDX = (INDX + 1);
            EXPT = (INDX as f64);
            testutil::CHCKSD(b"MOUT(J,I)", MOUT[[J, I]], b"=", EXPT, 0.0, OK, ctx)?;
        }
    }

    //

    testutil::TCASE(b"XPOSE: generating the transpose of a random matrix, and checking equality between original and transposed elements", ctx)?;

    spicelib::XPOSE(MRAN.as_slice(), MOUT.as_slice_mut());

    for J in 1..=N {
        for I in 1..=N {
            testutil::CHCKSD(b"MOUT(J,I)", MOUT[[J, I]], b"=", MRAN[[I, J]], 0.0, OK, ctx)?;
        }
    }

    //
    // Cases to test TRACE
    //

    testutil::TCASE(b"TRACE: an index matrix, whose trace is 15.0D0", ctx)?;

    EVAL = spicelib::TRACE(MINDX.as_slice());

    EXPT = 15.0;

    testutil::CHCKSD(b"EVAL", EVAL, b"=", EXPT, 0.0, OK, ctx)?;

    testutil::TCASE(b"TRACE: random matrix", ctx)?;

    EVAL = spicelib::TRACE(MRAN.as_slice());

    EXPT = 0.0;
    for I in 1..=N {
        EXPT = (EXPT + MRAN[[I, I]]);
    }

    testutil::CHCKSD(b"EVAL", EVAL, b"=", EXPT, 0.0, OK, ctx)?;

    //
    // Cases to test DET
    //

    testutil::TCASE(b"DET: random matrix", ctx)?;

    EVAL = spicelib::DET(MRAN.as_slice());

    //
    // The determinant of a 3x3x matrix can be computed by adding three
    // up diagonal followed by subtracting three down-diagonal products.
    //
    EXPT = (((((((MRAN[[1, 1]] * MRAN[[2, 2]]) * MRAN[[3, 3]])
        + ((MRAN[[1, 2]] * MRAN[[2, 3]]) * MRAN[[3, 1]]))
        + ((MRAN[[1, 3]] * MRAN[[2, 1]]) * MRAN[[3, 2]]))
        - ((MRAN[[1, 1]] * MRAN[[2, 3]]) * MRAN[[3, 2]]))
        - ((MRAN[[1, 2]] * MRAN[[2, 1]]) * MRAN[[3, 3]]))
        - ((MRAN[[1, 3]] * MRAN[[2, 2]]) * MRAN[[3, 1]]));

    testutil::CHCKSD(b"EVAL", EVAL, b"~", EXPT, VTIGHT, OK, ctx)?;

    //
    // Cases to test MXV
    //
    testutil::TCASE(b"MXV: random matrix and random vector", ctx)?;

    spicelib::MXV(MRAN.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());

    for I in 1..=N {
        for J in 1..=N {
            VTEMP[J] = MRAN[[I, J]];
        }
        VEXPT[I] = spicelib::VDOT(VTEMP.as_slice(), VRAN.as_slice());
    }

    testutil::CHCKAD(
        b"VOUT",
        VOUT.as_slice(),
        b"~",
        VEXPT.as_slice(),
        N,
        VTIGHT,
        OK,
        ctx,
    )?;

    //
    // Cases to test MTXV
    //
    testutil::TCASE(b"MTXV: random matrix and random vector", ctx)?;

    spicelib::MTXV(MRAN.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());

    for I in 1..=N {
        for J in 1..=N {
            VTEMP[J] = MRAN[[J, I]];
        }
        VEXPT[I] = spicelib::VDOT(VTEMP.as_slice(), VRAN.as_slice());
    }

    testutil::CHCKAD(
        b"VOUT",
        VOUT.as_slice(),
        b"=",
        VEXPT.as_slice(),
        N,
        0.0,
        OK,
        ctx,
    )?;

    //
    // Cases to test VTMV
    //
    testutil::TCASE(b"VTMV: random vector, random matrix, index vector ", ctx)?;

    EVAL = spicelib::VTMV(VRAN.as_slice(), MRAN.as_slice(), VINDX.as_slice());

    //
    // First compute the product of MRAN and VINDX.
    //
    spicelib::MXV(MRAN.as_slice(), VINDX.as_slice(), VTEMP.as_slice_mut());

    //
    // Then, compute the expected value using the routine VDOT.
    //
    EXPT = spicelib::VDOT(VRAN.as_slice(), VTEMP.as_slice());

    testutil::CHCKSD(b"EVAL", EVAL, b"~", EXPT, TIGHT, OK, ctx)?;

    //
    // Cases to test MXM
    //
    testutil::TCASE(b"MXM: Freivald\'s Algorithm", ctx)?;

    //
    // Compute the product MOUT = MRAN * MINDX.
    //
    spicelib::MXM(MRAN.as_slice(), MINDX.as_slice(), MOUT.as_slice_mut());

    //
    // Check MRAN * ( MINDX * VRAN ) = MOUT * VRAN, where VRAN is
    // an arbitrary vector.
    //
    spicelib::MXV(MINDX.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());
    spicelib::MXV(MRAN.as_slice(), VOUT.as_slice(), VTEMP.as_slice_mut());
    spicelib::MXV(MOUT.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());

    testutil::CHCKAD(
        b"VOUT",
        VOUT.as_slice(),
        b"~",
        VTEMP.as_slice(),
        N,
        TIGHT,
        OK,
        ctx,
    )?;

    //
    // Cases to test MTXM
    //
    testutil::TCASE(b"MTXM: Freivald\'s Algorithm", ctx)?;

    //
    // Compute the product MOUT = Transpose(MRAN) * MINDX.
    //
    spicelib::MTXM(MRAN.as_slice(), MINDX.as_slice(), MOUT.as_slice_mut());

    //
    // Check Transpose(MRAN) * ( MINDX * VRAN ) = MOUT * VRAN,
    // where VRAN is an arbitrary vector.
    //
    spicelib::MXV(MINDX.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());
    spicelib::MTXV(MRAN.as_slice(), VOUT.as_slice(), VTEMP.as_slice_mut());
    spicelib::MXV(MOUT.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());

    testutil::CHCKAD(
        b"VOUT",
        VOUT.as_slice(),
        b"~",
        VTEMP.as_slice(),
        N,
        TIGHT,
        OK,
        ctx,
    )?;

    //
    // Cases to test MXMT
    //
    testutil::TCASE(b"MXMT: Freivald\'s Algorithm", ctx)?;

    //
    // Compute the product MOUT = MRAN * Transpose(MINDX).
    //
    spicelib::MXMT(MRAN.as_slice(), MINDX.as_slice(), MOUT.as_slice_mut());

    //
    // Check MRAN * ( Transpose(MINDX) * VRAN ) = MOUT * VRAN,
    // where VRAN is an arbitrary vector.
    //
    spicelib::MTXV(MINDX.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());
    spicelib::MXV(MRAN.as_slice(), VOUT.as_slice(), VTEMP.as_slice_mut());
    spicelib::MXV(MOUT.as_slice(), VRAN.as_slice(), VOUT.as_slice_mut());

    testutil::CHCKAD(
        b"VOUT",
        VOUT.as_slice(),
        b"~",
        VTEMP.as_slice(),
        N,
        TIGHT,
        OK,
        ctx,
    )?;

    testutil::T_SUCCESS(OK, ctx);

    Ok(())
}