Skip to content

Commit 935cda7

Browse files
Add error handling for vector expressions
Fixes #58 and #106 by throwing an informative error when vector expressions are passed to integrate(). The error message guides users to use element-wise integration instead: integrate.([expr1, expr2, ...], x) Added tests to verify: - Vector expressions throw appropriate error - Scalar integration still works - Element-wise integration works as expected 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4a11131 commit 935cda7

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

simple_test.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Pkg
2+
Pkg.activate(".")
3+
4+
# Force load the package
5+
using SymbolicNumericIntegration
6+
using Symbolics
7+
8+
println("Testing vector expression error handling...")
9+
10+
# Test case 1: Vector with symbolic variables
11+
@variables x
12+
try
13+
result = integrate([x])
14+
println("❌ Test 1 FAILED: Should have thrown an error but got: $result")
15+
catch e
16+
if occursin("Vector expressions are not supported", string(e))
17+
println("✅ Test 1 PASSED: Vector input correctly rejected")
18+
else
19+
println("❌ Test 1 FAILED: Wrong error message: $(e)")
20+
end
21+
end
22+
23+
# Test case 2: Scalar integration should still work
24+
try
25+
result = integrate(x)
26+
println("✅ Test 2 PASSED: Scalar integration works: $result")
27+
catch e
28+
println("❌ Test 2 FAILED: Scalar integration should work but got: $(e)")
29+
end
30+
31+
println("\nAll tests completed!")

src/integral.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ function integrate(eq, x = nothing;
7272
detailed = true)
7373
deprecation_warnings(; homotopy, use_optim)
7474

75+
# Check if eq is a vector/array expression and throw an error
76+
if eq isa AbstractArray || eq isa AbstractVector
77+
error("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.")
78+
end
79+
7580
eq = expand(eq)
7681

7782
if x == nothing

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,21 @@ end
327327
num_trials = 10)
328328
@test n > 0
329329
end
330+
331+
@testset "vector expression error handling" begin
332+
@variables x α
333+
334+
# Test that vector expressions throw an appropriate error
335+
@test_throws ErrorException("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.") integrate([x])
336+
@test_throws ErrorException("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.") integrate([1, 2 * α], α)
337+
338+
# Test that scalar integration still works
339+
@test integrate(x) == ((1//2)*(x^2), 0, 0)
340+
@test integrate(2 * α, α) ==^2, 0, 0)
341+
342+
# Test that element-wise integration works
343+
results = integrate.([1, 2 * α], α)
344+
@test length(results) == 2
345+
@test results[1] == (α, 0, 0)
346+
@test results[2] ==^2, 0, 0)
347+
end

test_vector_fix.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Pkg
2+
Pkg.activate(".")
3+
using Symbolics, SymbolicNumericIntegration
4+
5+
println("Testing vector expression error handling:")
6+
println()
7+
8+
# Test case from issue #58
9+
@variables α
10+
exp2 = [1, 2 * α]
11+
12+
println("Test 1: integrate([1, 2α], α)")
13+
try
14+
result = integrate(exp2, α)
15+
println("ERROR: Should have thrown an error but got: $result")
16+
catch e
17+
println("✓ Correctly caught error: $(e)")
18+
end
19+
20+
println()
21+
println("Test 2: Verify element-wise integration still works")
22+
try
23+
result = integrate.(exp2, α)
24+
println("✓ Element-wise integration works: $result")
25+
catch e
26+
println("ERROR: Element-wise should work but got: $(e)")
27+
end
28+
29+
println()
30+
# Test case from issue #106
31+
@variables x
32+
println("Test 3: integrate([x])")
33+
try
34+
result = integrate([x])
35+
println("ERROR: Should have thrown an error but got: $result")
36+
catch e
37+
println("✓ Correctly caught error: $(e)")
38+
end
39+
40+
println()
41+
println("Test 4: Verify scalar integration still works")
42+
try
43+
result = integrate(2 * α, α)
44+
println("✓ Scalar integration works: $result")
45+
catch e
46+
println("ERROR: Scalar should work but got: $(e)")
47+
end

0 commit comments

Comments
 (0)