vedaksha-astro 3.1.0

Western astrology: house systems, aspects, dignities, chart computation
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
// Copyright © 2026 ArthIQ Labs LLC. All rights reserved.
// Vedākṣha — Vision from Vedas
// Licensed under BSL 1.1. See LICENSE file.
// Contact: info@arthiq.net | https://vedaksha.net

//! Astrological pattern detection.
//!
//! Detects Grand Trine, T-Square, Yod, Grand Cross, and Stellium patterns
//! from a set of pre-computed aspects and body positions.
//!
//! Source: Standard astrological pattern definitions.

use super::{Aspect, AspectType, BodyPosition};

/// An astrological pattern formed by multiple aspects.
#[derive(Debug, Clone)]
pub struct Pattern {
    /// The type of pattern.
    pub pattern_type: PatternType,
    /// Indices of the bodies forming this pattern.
    pub body_indices: Vec<usize>,
}

/// Type of multi-body astrological pattern.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternType {
    /// Three bodies in mutual trine (120° apart from each other).
    GrandTrine,
    /// Two bodies in opposition with a third squaring both.
    TSquare,
    /// Two bodies in sextile with a third quincunx to both.
    Yod,
    /// Four bodies forming a cross of oppositions and squares.
    GrandCross,
    /// Three or more bodies within conjunction orb of each other.
    Stellium,
}

/// Detect patterns from a list of aspects and body positions.
///
/// Returns all detected patterns. A single chart may contain multiple
/// instances of the same pattern type.
#[must_use]
pub fn detect_patterns(aspects: &[Aspect], positions: &[BodyPosition]) -> Vec<Pattern> {
    let mut patterns = Vec::new();

    detect_grand_trines(aspects, &mut patterns);
    detect_t_squares(aspects, &mut patterns);
    detect_yods(aspects, &mut patterns);
    detect_grand_crosses(aspects, &mut patterns);
    detect_stelliums(positions, &mut patterns);

    patterns
}

// ---------------------------------------------------------------------------
// Helper: check whether two bodies form a given aspect type in the aspect list
// ---------------------------------------------------------------------------

fn has_aspect(aspects: &[Aspect], i: usize, j: usize, kind: AspectType) -> bool {
    aspects.iter().any(|a| {
        a.aspect_type == kind
            && ((a.body1_index == i && a.body2_index == j)
                || (a.body1_index == j && a.body2_index == i))
    })
}

// ---------------------------------------------------------------------------
// Grand Trine: A-B trine, B-C trine, A-C trine
// ---------------------------------------------------------------------------

fn detect_grand_trines(aspects: &[Aspect], out: &mut Vec<Pattern>) {
    // Collect all unique body indices involved in at least one trine.
    let trine_bodies: Vec<usize> = {
        let mut v: Vec<usize> = aspects
            .iter()
            .filter(|a| a.aspect_type == AspectType::Trine)
            .flat_map(|a| [a.body1_index, a.body2_index])
            .collect();
        v.sort_unstable();
        v.dedup();
        v
    };

    let n = trine_bodies.len();
    for ai in 0..n {
        for bi in (ai + 1)..n {
            for ci in (bi + 1)..n {
                let a = trine_bodies[ai];
                let b = trine_bodies[bi];
                let c = trine_bodies[ci];

                if has_aspect(aspects, a, b, AspectType::Trine)
                    && has_aspect(aspects, b, c, AspectType::Trine)
                    && has_aspect(aspects, a, c, AspectType::Trine)
                {
                    out.push(Pattern {
                        pattern_type: PatternType::GrandTrine,
                        body_indices: vec![a, b, c],
                    });
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// T-Square: A-B opposition, C squares both A and B
// ---------------------------------------------------------------------------

fn detect_t_squares(aspects: &[Aspect], out: &mut Vec<Pattern>) {
    let oppositions: Vec<(usize, usize)> = aspects
        .iter()
        .filter(|a| a.aspect_type == AspectType::Opposition)
        .map(|a| (a.body1_index, a.body2_index))
        .collect();

    // Collect all body indices.
    let all_bodies: Vec<usize> = {
        let mut v: Vec<usize> = aspects
            .iter()
            .flat_map(|a| [a.body1_index, a.body2_index])
            .collect();
        v.sort_unstable();
        v.dedup();
        v
    };

    for (a, b) in &oppositions {
        for &c in &all_bodies {
            if c == *a || c == *b {
                continue;
            }
            if has_aspect(aspects, c, *a, AspectType::Square)
                && has_aspect(aspects, c, *b, AspectType::Square)
            {
                out.push(Pattern {
                    pattern_type: PatternType::TSquare,
                    body_indices: vec![*a, *b, c],
                });
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Yod (Finger of God): A-B sextile, C quincunx to both A and B
// ---------------------------------------------------------------------------

fn detect_yods(aspects: &[Aspect], out: &mut Vec<Pattern>) {
    let sextiles: Vec<(usize, usize)> = aspects
        .iter()
        .filter(|a| a.aspect_type == AspectType::Sextile)
        .map(|a| (a.body1_index, a.body2_index))
        .collect();

    let all_bodies: Vec<usize> = {
        let mut v: Vec<usize> = aspects
            .iter()
            .flat_map(|a| [a.body1_index, a.body2_index])
            .collect();
        v.sort_unstable();
        v.dedup();
        v
    };

    for (a, b) in &sextiles {
        for &c in &all_bodies {
            if c == *a || c == *b {
                continue;
            }
            if has_aspect(aspects, c, *a, AspectType::Quincunx)
                && has_aspect(aspects, c, *b, AspectType::Quincunx)
            {
                out.push(Pattern {
                    pattern_type: PatternType::Yod,
                    body_indices: vec![*a, *b, c],
                });
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Grand Cross: four bodies forming two pairs of oppositions with squares
// ---------------------------------------------------------------------------

fn detect_grand_crosses(aspects: &[Aspect], out: &mut Vec<Pattern>) {
    // Collect opposition pairs.
    let oppositions: Vec<(usize, usize)> = aspects
        .iter()
        .filter(|a| a.aspect_type == AspectType::Opposition)
        .map(|a| (a.body1_index, a.body2_index))
        .collect();

    let opp_count = oppositions.len();
    for i in 0..opp_count {
        for j in (i + 1)..opp_count {
            let (body_a, body_b) = oppositions[i];
            let (body_c, body_d) = oppositions[j];

            // All four bodies must be distinct.
            let mut indices = [body_a, body_b, body_c, body_d];
            indices.sort_unstable();
            if indices[0] == indices[1] || indices[1] == indices[2] || indices[2] == indices[3] {
                continue;
            }

            // The two pairs must form squares with each other.
            // In a Grand Cross: body_a squares body_c, body_a squares body_d, etc.
            if has_aspect(aspects, body_a, body_c, AspectType::Square)
                && has_aspect(aspects, body_a, body_d, AspectType::Square)
                && has_aspect(aspects, body_b, body_c, AspectType::Square)
                && has_aspect(aspects, body_b, body_d, AspectType::Square)
            {
                out.push(Pattern {
                    pattern_type: PatternType::GrandCross,
                    body_indices: vec![body_a, body_b, body_c, body_d],
                });
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Stellium: three or more bodies all within conjunction orb of each other
// ---------------------------------------------------------------------------

fn detect_stelliums(positions: &[BodyPosition], out: &mut Vec<Pattern>) {
    let orb = AspectType::Conjunction.default_orb();
    let n = positions.len();

    // Build a graph of which pairs are within conjunction orb.
    // Then find all maximal cliques of size >= 3.
    let mut adjacent = vec![vec![false; n]; n];
    for i in 0..n {
        for j in (i + 1)..n {
            let sep = vedaksha_math::angle::angular_separation(
                positions[i].longitude,
                positions[j].longitude,
            );
            if sep <= orb {
                adjacent[i][j] = true;
                adjacent[j][i] = true;
            }
        }
    }

    // Enumerate all subsets of size >= 3 that form cliques.
    // For typical chart sizes (10-20 bodies) this is fine.
    for size in 3..=n {
        find_cliques_of_size(&adjacent, n, size, out);
    }
}

/// Find all cliques of exactly `size` in the adjacency matrix and emit them as
/// Stellium patterns. Only add if not already a sub-clique of a larger one
/// (we use a simple deduplcation: skip if we have already emitted a superset).
fn find_cliques_of_size(adjacent: &[Vec<bool>], n: usize, size: usize, out: &mut Vec<Pattern>) {
    // Generate combinations of `size` indices.
    let mut combo: Vec<usize> = (0..size).collect();

    loop {
        // Check if combo is a clique.
        let is_clique = combo.windows(2).all(|_| true) && {
            let mut ok = true;
            'outer: for ai in 0..combo.len() {
                for bi in (ai + 1)..combo.len() {
                    if !adjacent[combo[ai]][combo[bi]] {
                        ok = false;
                        break 'outer;
                    }
                }
            }
            ok
        };

        if is_clique {
            // Only add if not already subsumed by an existing larger stellium.
            let already_covered = out.iter().any(|p| {
                p.pattern_type == PatternType::Stellium
                    && combo.iter().all(|idx| p.body_indices.contains(idx))
            });
            if !already_covered {
                out.push(Pattern {
                    pattern_type: PatternType::Stellium,
                    body_indices: combo.clone(),
                });
            }
        }

        // Advance to next combination.
        if !next_combination(&mut combo, n) {
            break;
        }
    }
}

/// Advance a sorted combination to the next one in lexicographic order.
/// Returns `false` when we have exhausted all combinations.
fn next_combination(combo: &mut [usize], n: usize) -> bool {
    let k = combo.len();
    // Find rightmost element that can be incremented.
    let mut i = k;
    loop {
        if i == 0 {
            return false;
        }
        i -= 1;
        if combo[i] < n - k + i {
            combo[i] += 1;
            for j in (i + 1)..k {
                combo[j] = combo[j - 1] + 1;
            }
            return true;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aspects::{BodyPosition, find_aspects};

    fn pos(longitude: f64) -> BodyPosition {
        BodyPosition {
            longitude,
            speed: 1.0,
        }
    }

    // --- Grand Trine ---

    #[test]
    fn detects_grand_trine() {
        // Three bodies exactly 120° apart.
        let positions = [pos(0.0), pos(120.0), pos(240.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::GrandTrine),
            "Expected Grand Trine"
        );
    }

    // --- Stellium ---

    #[test]
    fn detects_stellium_three_bodies() {
        // Three bodies very close together.
        let positions = [pos(10.0), pos(12.0), pos(14.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::Stellium),
            "Expected Stellium"
        );
    }

    #[test]
    fn stellium_contains_correct_indices() {
        let positions = [pos(10.0), pos(12.0), pos(14.0), pos(200.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        let stellium = patterns
            .iter()
            .find(|p| p.pattern_type == PatternType::Stellium)
            .expect("Expected a Stellium");
        // Body at index 3 (200°) should NOT be in the stellium.
        assert!(
            !stellium.body_indices.contains(&3),
            "Body 3 should not be in the stellium"
        );
        // Bodies 0,1,2 should all be in the stellium.
        assert!(stellium.body_indices.contains(&0));
        assert!(stellium.body_indices.contains(&1));
        assert!(stellium.body_indices.contains(&2));
    }

    #[test]
    fn no_stellium_when_bodies_spread_out() {
        let positions = [pos(0.0), pos(60.0), pos(120.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            !patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::Stellium),
            "Should not find stellium when bodies are spread"
        );
    }

    // --- T-Square ---

    #[test]
    fn detects_t_square() {
        // A at 0°, B at 180° (opposition), C at 90° (squares both).
        let positions = [pos(0.0), pos(180.0), pos(90.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::TSquare),
            "Expected T-Square"
        );
    }

    // --- Yod ---

    #[test]
    fn detects_yod() {
        // A at 0°, B at 60° (sextile), C at 150° (quincunx to A=150°, quincunx to B=90°).
        // C must be quincunx (150°) to both A and B.
        // sep(C=210°, A=0°) = 150° ✓, sep(C=210°, B=60°) = 150° ✓
        let positions = [pos(0.0), pos(60.0), pos(210.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            patterns.iter().any(|p| p.pattern_type == PatternType::Yod),
            "Expected Yod"
        );
    }

    // --- Grand Cross ---

    #[test]
    fn detects_grand_cross() {
        // Four bodies: 0°, 90°, 180°, 270° forming a perfect cross.
        let positions = [pos(0.0), pos(90.0), pos(180.0), pos(270.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::GrandCross),
            "Expected Grand Cross"
        );
    }

    // --- no false positives ---

    #[test]
    fn no_grand_trine_for_random_positions() {
        let positions = [pos(0.0), pos(45.0), pos(200.0)];
        let aspects = find_aspects(&positions, AspectType::ALL, 1.0);
        let patterns = detect_patterns(&aspects, &positions);
        assert!(
            !patterns
                .iter()
                .any(|p| p.pattern_type == PatternType::GrandTrine),
            "Should not find Grand Trine for random positions"
        );
    }
}