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
//! Integration tests for calculus_util — symbolic domain analysis.
//!
//! These tests exercise `continuous_domain`, `singularities`, and
//! `estimate_frequency` through the public `Context` / `Ex` API where
//! possible, and validate domain-related behaviour end-to-end.
use symplex::prelude::*;
// ═══════════════════════════════════════════════════════════════════════════
// continuous_domain — via public inequality API as proxy checks
// ═══════════════════════════════════════════════════════════════════════════
/// sqrt(x) requires x ≥ 0. Verify via solve_ge that x ≥ 0 yields [0, ∞).
#[test]
fn domain_sqrt_x_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
// x ≥ 0 should give [0, ∞)
let result = x.solve_ge(&x);
let s = format!("{result}");
assert!(!s.contains("EmptySet"), "x >= 0 should not be empty: {s}");
assert!(s.contains("0"), "x >= 0 domain should reference 0: {s}");
}
/// 1/x is undefined at x = 0. Verify that solving x = 0 finds the singularity.
#[test]
fn domain_1_over_x_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
// Solve x = 0 → {0}
let roots = x.solve_as_set(&x);
let s = format!("{roots}");
assert!(s.contains("0"), "solve x=0 should find root 0: {s}");
}
/// ln(x) requires x > 0. Verify via solve_gt.
#[test]
fn domain_ln_x_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
// x > 0 → (0, ∞)
let result = x.solve_gt(&x);
let s = format!("{result}");
assert!(!s.contains("EmptySet"), "x > 0 should not be empty: {s}");
assert!(
s.contains("0") && (s.contains("oo") || s.contains("∞")),
"x > 0 should give (0, ∞): {s}"
);
}
/// sqrt(x - 2) on [-5, 5] requires x - 2 ≥ 0 → x ≥ 2.
/// Verify that x - 2 ≥ 0 gives x ∈ [2, ∞), which intersected with
/// [-5, 5] yields [2, 5].
#[test]
fn domain_sqrt_x_minus_2_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
let two = ctx.int(2);
let inner = &x - &two; // x - 2
// x - 2 ≥ 0
let result = inner.solve_ge(&x);
let s = format!("{result}");
assert!(
s.contains("2"),
"x - 2 >= 0 should reference 2 as boundary: {s}"
);
}
// ═══════════════════════════════════════════════════════════════════════════
// singularities — end-to-end checks through solve
// ═══════════════════════════════════════════════════════════════════════════
/// tan(x) has singularities where cos(x) = 0.
/// Verify that cos(x) has roots near π/2.
#[test]
fn singularities_tan_x_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
let cos_x = x.cos();
// Try to find where cos(x) = 0
let roots = cos_x.solve(&x).unwrap_or_default();
// cos(x) = 0 is transcendental; solver may or may not find roots.
// If it does, verify they're near π/2 + nπ.
for root in &roots {
let s = format!("{root}");
// If it solved, the root should involve pi
if s.contains("pi") || s.contains("π") {
// Good — it found a symbolic root involving pi
return;
}
}
// Even if the solver can't find symbolic roots, tan(x) is still
// known to have singularities — this is a best-effort check.
}
/// 1/x has a singularity at x = 0.
#[test]
fn singularities_1_over_x_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
// The denominator is x; solve x = 0
let roots = x.solve(&x).unwrap_or_default();
assert!(!roots.is_empty(), "x = 0 should have a solution");
let root_s = format!("{}", roots[0]);
assert!(root_s == "0", "x = 0 root should be 0, got: {root_s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// estimate_frequency — structural checks
// ═══════════════════════════════════════════════════════════════════════════
/// sin(100*x) should have angular frequency 100.
/// We verify the structure: 100*x inside sin.
#[test]
fn frequency_sin_100x_structure() {
let ctx = Context::new();
let x = ctx.symbol("x");
let hundred = ctx.int(100);
let inner = &hundred * &x;
let expr = inner.sin();
// Verify the expression is well-formed
let s = format!("{expr}");
assert!(
s.contains("sin") && s.contains("100"),
"sin(100*x) should display as such: {s}"
);
}
/// x^2 + 1 has no trig terms, so no frequency.
#[test]
fn frequency_no_trig_structure() {
let ctx = Context::new();
let x = ctx.symbol("x");
let expr = &x.powi(2) + 1;
let s = format!("{expr}");
assert!(
!s.contains("sin") && !s.contains("cos") && !s.contains("tan"),
"x^2 + 1 should have no trig terms: {s}"
);
}
// ═══════════════════════════════════════════════════════════════════════════
// Additional domain edge cases
// ═══════════════════════════════════════════════════════════════════════════
/// 1/(x^2 + 1) has no real singularities — denominator is always ≥ 1.
#[test]
fn domain_1_over_x2_plus_1_no_restriction() {
let ctx = Context::new();
let x = ctx.symbol("x");
// x^2 + 1 = 0 has no real roots
let denom = &x.powi(2) + 1;
let roots = denom.solve(&x).unwrap_or_default();
// Either no roots, or only complex roots
// (display of complex roots will contain "I" or "i")
for root in &roots {
let s = format!("{root}");
assert!(
s.contains("I") || s.contains("i"),
"x^2+1 should only have complex roots, got real root: {s}"
);
}
}
/// Verify that negative constant exponent triggers domain restriction.
/// 1/x^2 = x^(-2) should exclude x = 0.
#[test]
fn domain_x_pow_neg2_excludes_zero() {
let ctx = Context::new();
let x = ctx.symbol("x");
// x = 0 is in the zero set of x (the base)
let roots = x.solve(&x).unwrap_or_default();
assert!(!roots.is_empty(), "x = 0 should have solution 0");
assert_eq!(format!("{}", roots[0]), "0");
}
/// Nested expression: ln(x^2 - 1) requires x^2 - 1 > 0.
#[test]
fn domain_ln_x2_minus_1_proxy() {
let ctx = Context::new();
let x = ctx.symbol("x");
let inner = &x.powi(2) - 1; // x^2 - 1
// x^2 - 1 > 0 → x < -1 or x > 1
let result = inner.solve_gt(&x);
let s = format!("{result}");
assert!(
!s.contains("EmptySet"),
"x^2-1 > 0 should have solutions: {s}"
);
// Should reference the boundary points ±1
assert!(
s.contains("1"),
"x^2-1 > 0 should reference boundary 1: {s}"
);
}
/// cos(3*x) should yield angular frequency 3.
#[test]
fn frequency_cos_3x_structure() {
let ctx = Context::new();
let x = ctx.symbol("x");
let three = ctx.int(3);
let inner = &three * &x;
let expr = inner.cos();
let s = format!("{expr}");
assert!(
s.contains("cos") && s.contains("3"),
"cos(3*x) should display correctly: {s}"
);
}
/// Multiple trig terms: sin(5*x) + cos(10*x).
/// The maximum frequency should come from cos(10*x).
#[test]
fn frequency_max_of_multiple_trig() {
let ctx = Context::new();
let x = ctx.symbol("x");
let five = ctx.int(5);
let ten = ctx.int(10);
let term1 = (&five * &x).sin();
let term2 = (&ten * &x).cos();
let expr = &term1 + &term2;
let s = format!("{expr}");
// Both trig terms should be present
assert!(
s.contains("sin") && s.contains("cos"),
"sin(5x) + cos(10x) should contain both trig fns: {s}"
);
}