rust_physics_engine 0.1.0

A comprehensive, zero-dependency Rust library for physics, mathematics, and engineering computation — 1,600+ validated functions covering 50+ domains
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
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
use crate::math::constants::PI;

const TWO_PI: f64 = 2.0 * PI;
const HALF_PI: f64 = PI / 2.0;

// --- Triangle Laws ---

/// Compute unknown side via law of cosines: c = sqrt(a² + b² - 2ab·cos(C))
#[must_use]
pub fn law_of_cosines_side(a: f64, b: f64, angle_c: f64) -> f64 {
    (a * a + b * b - 2.0 * a * b * angle_c.cos()).sqrt()
}

/// Compute unknown angle via law of cosines: C = arccos((a² + b² - c²) / (2ab))
#[must_use]
pub fn law_of_cosines_angle(a: f64, b: f64, c: f64) -> f64 {
    let numerator = a * a + b * b - c * c;
    let denominator = 2.0 * a * b;
    (numerator / denominator).clamp(-1.0, 1.0).acos()
}

/// Compute unknown side via law of sines: b = a·sin(B) / sin(A)
#[must_use]
pub fn law_of_sines_side(a: f64, angle_a: f64, angle_b: f64) -> f64 {
    a * angle_b.sin() / angle_a.sin()
}

/// Compute unknown angle via law of sines: B = arcsin(b·sin(A) / a)
#[must_use]
pub fn law_of_sines_angle(a: f64, b: f64, angle_a: f64) -> f64 {
    (b * angle_a.sin() / a).clamp(-1.0, 1.0).asin()
}

/// Triangle area using two sides and included angle: A = ½·a·b·sin(C)
#[must_use]
pub fn triangle_area_sas(a: f64, b: f64, angle_c: f64) -> f64 {
    0.5 * a * b * angle_c.sin()
}

// --- Trig Identities ---

/// Sine of sum identity: sin(a+b) = sin(a)cos(b) + cos(a)sin(b)
#[must_use]
pub fn sin_sum(a: f64, b: f64) -> f64 {
    a.sin() * b.cos() + a.cos() * b.sin()
}

/// Cosine of sum identity: cos(a+b) = cos(a)cos(b) - sin(a)sin(b)
#[must_use]
pub fn cos_sum(a: f64, b: f64) -> f64 {
    a.cos() * b.cos() - a.sin() * b.sin()
}

/// Sine of difference identity: sin(a-b) = sin(a)cos(b) - cos(a)sin(b)
#[must_use]
pub fn sin_diff(a: f64, b: f64) -> f64 {
    a.sin() * b.cos() - a.cos() * b.sin()
}

/// Cosine of difference identity: cos(a-b) = cos(a)cos(b) + sin(a)sin(b)
#[must_use]
pub fn cos_diff(a: f64, b: f64) -> f64 {
    a.cos() * b.cos() + a.sin() * b.sin()
}

/// Tangent of sum identity: tan(a+b) = (tan(a) + tan(b)) / (1 - tan(a)tan(b))
#[must_use]
pub fn tan_sum(a: f64, b: f64) -> f64 {
    let tan_a = a.tan();
    let tan_b = b.tan();
    (tan_a + tan_b) / (1.0 - tan_a * tan_b)
}

/// Double-angle sine identity: sin(2a) = 2·sin(a)·cos(a)
#[must_use]
pub fn double_angle_sin(a: f64) -> f64 {
    2.0 * a.sin() * a.cos()
}

/// Double-angle cosine identity: cos(2a) = cos²(a) - sin²(a)
#[must_use]
pub fn double_angle_cos(a: f64) -> f64 {
    let c = a.cos();
    let s = a.sin();
    c * c - s * s
}

/// Half-angle sine identity: sin(a/2) = sqrt(|1 - cos(a)| / 2)
#[must_use]
pub fn half_angle_sin(a: f64) -> f64 {
    ((1.0 - a.cos()) / 2.0).abs().sqrt()
}

/// Half-angle cosine identity: cos(a/2) = sqrt(|1 + cos(a)| / 2)
#[must_use]
pub fn half_angle_cos(a: f64) -> f64 {
    ((1.0 + a.cos()) / 2.0).abs().sqrt()
}

/// Product-to-sum for sin·sin: sin(a)sin(b) = ½[cos(a-b) - cos(a+b)]
#[must_use]
pub fn product_to_sum_sin_sin(a: f64, b: f64) -> f64 {
    0.5 * ((a - b).cos() - (a + b).cos())
}

/// Product-to-sum for cos·cos: cos(a)cos(b) = ½[cos(a-b) + cos(a+b)]
#[must_use]
pub fn product_to_sum_cos_cos(a: f64, b: f64) -> f64 {
    0.5 * ((a - b).cos() + (a + b).cos())
}

// --- Hyperbolic Functions ---

/// Hyperbolic sine: sinh(x) = (eˣ - e⁻ˣ) / 2
#[must_use]
pub fn sinh(x: f64) -> f64 {
    x.sinh()
}

/// Hyperbolic cosine: cosh(x) = (eˣ + e⁻ˣ) / 2
#[must_use]
pub fn cosh(x: f64) -> f64 {
    x.cosh()
}

/// Hyperbolic tangent: tanh(x) = sinh(x) / cosh(x)
#[must_use]
pub fn tanh(x: f64) -> f64 {
    x.tanh()
}

/// Hyperbolic secant: sech(x) = 1 / cosh(x)
#[must_use]
pub fn sech(x: f64) -> f64 {
    1.0 / x.cosh()
}

/// Hyperbolic cosecant: csch(x) = 1 / sinh(x)
#[must_use]
pub fn csch(x: f64) -> f64 {
    1.0 / x.sinh()
}

/// Hyperbolic cotangent: coth(x) = cosh(x) / sinh(x)
#[must_use]
pub fn coth(x: f64) -> f64 {
    x.cosh() / x.sinh()
}

/// Inverse hyperbolic sine: asinh(x) = ln(x + sqrt(x² + 1))
#[must_use]
pub fn asinh(x: f64) -> f64 {
    (x + (x * x + 1.0).sqrt()).ln()
}

/// Inverse hyperbolic cosine: acosh(x) = ln(x + sqrt(x² - 1))
#[must_use]
pub fn acosh(x: f64) -> f64 {
    (x + (x * x - 1.0).sqrt()).ln()
}

/// Inverse hyperbolic tangent: atanh(x) = ½·ln((1+x) / (1-x))
#[must_use]
pub fn atanh(x: f64) -> f64 {
    0.5 * ((1.0 + x) / (1.0 - x)).ln()
}

// --- Angle Utilities ---

/// Normalize angle to the range [0, 2π)
#[must_use]
pub fn normalize_angle(angle: f64) -> f64 {
    let mut a = angle % TWO_PI;
    if a < 0.0 {
        a += TWO_PI;
    }
    a
}

/// Normalize angle to the range [-π, π)
#[must_use]
pub fn normalize_angle_signed(angle: f64) -> f64 {
    let mut a = angle % TWO_PI;
    if a >= PI {
        a -= TWO_PI;
    } else if a < -PI {
        a += TWO_PI;
    }
    a
}

/// Signed shortest angular difference from a to b: normalize(b - a) in [-π, π)
#[must_use]
pub fn angular_difference(a: f64, b: f64) -> f64 {
    normalize_angle_signed(b - a)
}

/// Check whether angle (in radians) is acute: 0 < angle < π/2
#[must_use]
pub fn is_acute(angle: f64) -> bool {
    let a = normalize_angle(angle);
    a > 0.0 && a < HALF_PI
}

/// Check whether angle (in radians) is a right angle within tolerance: |angle - π/2| < tol
#[must_use]
pub fn is_right(angle: f64, tolerance: f64) -> bool {
    (normalize_angle(angle) - HALF_PI).abs() < tolerance
}

/// Check whether angle (in radians) is obtuse: π/2 < angle < π
#[must_use]
pub fn is_obtuse(angle: f64) -> bool {
    let a = normalize_angle(angle);
    a > HALF_PI && a < PI
}

/// Complementary angle: π/2 - angle
#[must_use]
pub fn complementary(angle: f64) -> f64 {
    HALF_PI - angle
}

/// Supplementary angle: π - angle
#[must_use]
pub fn supplementary(angle: f64) -> f64 {
    PI - angle
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f64::consts::FRAC_PI_2;

    const EPSILON: f64 = 1e-10;

    fn approx(a: f64, b: f64) -> bool {
        (a - b).abs() < EPSILON
    }

    // --- Triangle Laws (30-60-90 triangle) ---
    // Sides: 1, sqrt(3), 2. Angles: PI/6, PI/3, PI/2.

    #[test]
    fn test_law_of_cosines_side_30_60_90() {
        let a = 1.0;
        let b = 3.0_f64.sqrt();
        let angle_c = FRAC_PI_2;
        let c = law_of_cosines_side(a, b, angle_c);
        assert!(approx(c, 2.0));
    }

    #[test]
    fn test_law_of_cosines_angle_30_60_90() {
        let a = 1.0;
        let b = 3.0_f64.sqrt();
        let c = 2.0;
        let angle = law_of_cosines_angle(a, b, c);
        assert!(approx(angle, FRAC_PI_2));
    }

    #[test]
    fn test_law_of_sines_side() {
        let a = 1.0;
        let angle_a = PI / 6.0;
        let angle_b = PI / 3.0;
        let b = law_of_sines_side(a, angle_a, angle_b);
        assert!(approx(b, 3.0_f64.sqrt()));
    }

    #[test]
    fn test_law_of_sines_angle() {
        let a = 2.0;
        let b = 1.0;
        let angle_a = FRAC_PI_2;
        let angle_b = law_of_sines_angle(a, b, angle_a);
        assert!(approx(angle_b, PI / 6.0));
    }

    #[test]
    fn test_triangle_area_sas() {
        let a = 1.0;
        let b = 3.0_f64.sqrt();
        let angle_c = FRAC_PI_2;
        let area = triangle_area_sas(a, b, angle_c);
        assert!(approx(area, 3.0_f64.sqrt() / 2.0));
    }

    // --- Identities vs std lib ---

    #[test]
    fn test_sin_sum() {
        let a = PI / 5.0;
        let b = PI / 7.0;
        assert!(approx(sin_sum(a, b), (a + b).sin()));
    }

    #[test]
    fn test_cos_sum() {
        let a = PI / 5.0;
        let b = PI / 7.0;
        assert!(approx(cos_sum(a, b), (a + b).cos()));
    }

    #[test]
    fn test_sin_diff() {
        let a = PI / 3.0;
        let b = PI / 4.0;
        assert!(approx(sin_diff(a, b), (a - b).sin()));
    }

    #[test]
    fn test_cos_diff() {
        let a = PI / 3.0;
        let b = PI / 4.0;
        assert!(approx(cos_diff(a, b), (a - b).cos()));
    }

    #[test]
    fn test_tan_sum() {
        let a = PI / 5.0;
        let b = PI / 7.0;
        assert!(approx(tan_sum(a, b), (a + b).tan()));
    }

    #[test]
    fn test_double_angle_sin() {
        let a = PI / 5.0;
        assert!(approx(double_angle_sin(a), (2.0 * a).sin()));
    }

    #[test]
    fn test_double_angle_cos() {
        let a = PI / 5.0;
        assert!(approx(double_angle_cos(a), (2.0 * a).cos()));
    }

    #[test]
    fn test_half_angle_sin() {
        let a = PI / 3.0;
        assert!(approx(half_angle_sin(a), (a / 2.0).sin()));
    }

    #[test]
    fn test_half_angle_cos() {
        let a = PI / 3.0;
        assert!(approx(half_angle_cos(a), (a / 2.0).cos()));
    }

    #[test]
    fn test_product_to_sum_sin_sin() {
        let a = PI / 4.0;
        let b = PI / 6.0;
        assert!(approx(product_to_sum_sin_sin(a, b), a.sin() * b.sin()));
    }

    #[test]
    fn test_product_to_sum_cos_cos() {
        let a = PI / 4.0;
        let b = PI / 6.0;
        assert!(approx(product_to_sum_cos_cos(a, b), a.cos() * b.cos()));
    }

    // --- Hyperbolic ---

    #[test]
    fn test_sinh_cosh_identity() {
        let x = 1.5;
        let s = sinh(x);
        let c = cosh(x);
        assert!(approx(c * c - s * s, 1.0));
    }

    #[test]
    fn test_tanh() {
        let x = 0.8;
        assert!(approx(tanh(x), x.tanh()));
    }

    #[test]
    fn test_sech() {
        let x = 1.0;
        assert!(approx(sech(x), 1.0 / x.cosh()));
    }

    #[test]
    fn test_csch() {
        let x = 1.0;
        assert!(approx(csch(x), 1.0 / x.sinh()));
    }

    #[test]
    fn test_coth() {
        let x = 1.0;
        assert!(approx(coth(x), x.cosh() / x.sinh()));
    }

    #[test]
    fn test_asinh_roundtrip() {
        let x = 2.5;
        assert!(approx(sinh(asinh(x)), x));
    }

    #[test]
    fn test_acosh_roundtrip() {
        let x = 3.0;
        assert!(approx(cosh(acosh(x)), x));
    }

    #[test]
    fn test_atanh_roundtrip() {
        let x = 0.7;
        assert!(approx(tanh(atanh(x)), x));
    }

    // --- Angle Utilities ---

    #[test]
    fn test_normalize_angle() {
        assert!(approx(normalize_angle(-PI / 2.0), 3.0 * FRAC_PI_2));
        assert!(approx(normalize_angle(3.0 * PI), PI));
        assert!(approx(normalize_angle(0.0), 0.0));
    }

    #[test]
    fn test_normalize_angle_signed() {
        assert!(approx(normalize_angle_signed(3.0 * PI / 2.0), -FRAC_PI_2));
        assert!(approx(normalize_angle_signed(-PI / 4.0), -PI / 4.0));
    }

    #[test]
    fn test_angular_difference() {
        assert!(approx(angular_difference(0.1, 0.2), 0.1));
        assert!(approx(angular_difference(0.0, 2.0 * PI - 0.1), -0.1));
    }

    #[test]
    fn test_is_acute() {
        assert!(is_acute(PI / 4.0));
        assert!(!is_acute(PI / 2.0));
        assert!(!is_acute(PI));
    }

    #[test]
    fn test_is_right() {
        assert!(is_right(FRAC_PI_2, 1e-9));
        assert!(!is_right(PI / 4.0, 1e-9));
    }

    #[test]
    fn test_is_obtuse() {
        assert!(is_obtuse(2.0 * PI / 3.0));
        assert!(!is_obtuse(PI / 4.0));
        assert!(!is_obtuse(PI));
    }

    #[test]
    fn test_complementary() {
        assert!(approx(complementary(PI / 6.0), PI / 3.0));
    }

    #[test]
    fn test_supplementary() {
        assert!(approx(supplementary(PI / 3.0), 2.0 * PI / 3.0));
    }

    #[test]
    fn test_normalize_angle_signed_very_negative() {
        let a = normalize_angle_signed(-4.0 * PI - 0.1);
        assert!(a >= -PI && a < PI, "got {a}");
    }

    #[test]
    fn test_normalize_angle_signed_below_neg_pi() {
        let a = normalize_angle_signed(-PI - 0.5);
        assert!(a >= -PI && a < PI, "got {a}");
        assert!((a - (PI - 0.5)).abs() < 1e-10);
    }
}