From 935cda79b9ba6be6d86fc3725075cfdab980dfb2 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas Date: Sun, 28 Sep 2025 17:25:21 -0400 Subject: [PATCH 1/2] Add error handling for vector expressions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- simple_test.jl | 31 ++++++++++++++++++++++++++++++ src/integral.jl | 5 +++++ test/runtests.jl | 18 ++++++++++++++++++ test_vector_fix.jl | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 simple_test.jl create mode 100644 test_vector_fix.jl diff --git a/simple_test.jl b/simple_test.jl new file mode 100644 index 0000000..9f35772 --- /dev/null +++ b/simple_test.jl @@ -0,0 +1,31 @@ +using Pkg +Pkg.activate(".") + +# Force load the package +using SymbolicNumericIntegration +using Symbolics + +println("Testing vector expression error handling...") + +# Test case 1: Vector with symbolic variables +@variables x +try + result = integrate([x]) + println("❌ Test 1 FAILED: Should have thrown an error but got: $result") +catch e + if occursin("Vector expressions are not supported", string(e)) + println("✅ Test 1 PASSED: Vector input correctly rejected") + else + println("❌ Test 1 FAILED: Wrong error message: $(e)") + end +end + +# Test case 2: Scalar integration should still work +try + result = integrate(x) + println("✅ Test 2 PASSED: Scalar integration works: $result") +catch e + println("❌ Test 2 FAILED: Scalar integration should work but got: $(e)") +end + +println("\nAll tests completed!") \ No newline at end of file diff --git a/src/integral.jl b/src/integral.jl index 04f9d04..3db34da 100644 --- a/src/integral.jl +++ b/src/integral.jl @@ -72,6 +72,11 @@ function integrate(eq, x = nothing; detailed = true) deprecation_warnings(; homotopy, use_optim) + # Check if eq is a vector/array expression and throw an error + if eq isa AbstractArray || eq isa AbstractVector + error("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.") + end + eq = expand(eq) if x == nothing diff --git a/test/runtests.jl b/test/runtests.jl index b4bc8a2..f2c14db 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -327,3 +327,21 @@ end num_trials = 10) @test n > 0 end + +@testset "vector expression error handling" begin + @variables x α + + # Test that vector expressions throw an appropriate error + @test_throws ErrorException("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.") integrate([x]) + @test_throws ErrorException("Vector expressions are not supported. Please use element-wise integration with `integrate.([expr1, expr2, ...], x)` instead.") integrate([1, 2 * α], α) + + # Test that scalar integration still works + @test integrate(x) == ((1//2)*(x^2), 0, 0) + @test integrate(2 * α, α) == (α^2, 0, 0) + + # Test that element-wise integration works + results = integrate.([1, 2 * α], α) + @test length(results) == 2 + @test results[1] == (α, 0, 0) + @test results[2] == (α^2, 0, 0) +end diff --git a/test_vector_fix.jl b/test_vector_fix.jl new file mode 100644 index 0000000..593aa67 --- /dev/null +++ b/test_vector_fix.jl @@ -0,0 +1,47 @@ +using Pkg +Pkg.activate(".") +using Symbolics, SymbolicNumericIntegration + +println("Testing vector expression error handling:") +println() + +# Test case from issue #58 +@variables α +exp2 = [1, 2 * α] + +println("Test 1: integrate([1, 2α], α)") +try + result = integrate(exp2, α) + println("ERROR: Should have thrown an error but got: $result") +catch e + println("✓ Correctly caught error: $(e)") +end + +println() +println("Test 2: Verify element-wise integration still works") +try + result = integrate.(exp2, α) + println("✓ Element-wise integration works: $result") +catch e + println("ERROR: Element-wise should work but got: $(e)") +end + +println() +# Test case from issue #106 +@variables x +println("Test 3: integrate([x])") +try + result = integrate([x]) + println("ERROR: Should have thrown an error but got: $result") +catch e + println("✓ Correctly caught error: $(e)") +end + +println() +println("Test 4: Verify scalar integration still works") +try + result = integrate(2 * α, α) + println("✓ Scalar integration works: $result") +catch e + println("ERROR: Scalar should work but got: $(e)") +end \ No newline at end of file From 708e3205a937ecc41e1278e52db49d7f4a7ab5b2 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas Date: Sun, 28 Sep 2025 17:26:46 -0400 Subject: [PATCH 2/2] Clean up temporary test files --- simple_test.jl | 31 ------------------------------ test_vector_fix.jl | 47 ---------------------------------------------- 2 files changed, 78 deletions(-) delete mode 100644 simple_test.jl delete mode 100644 test_vector_fix.jl diff --git a/simple_test.jl b/simple_test.jl deleted file mode 100644 index 9f35772..0000000 --- a/simple_test.jl +++ /dev/null @@ -1,31 +0,0 @@ -using Pkg -Pkg.activate(".") - -# Force load the package -using SymbolicNumericIntegration -using Symbolics - -println("Testing vector expression error handling...") - -# Test case 1: Vector with symbolic variables -@variables x -try - result = integrate([x]) - println("❌ Test 1 FAILED: Should have thrown an error but got: $result") -catch e - if occursin("Vector expressions are not supported", string(e)) - println("✅ Test 1 PASSED: Vector input correctly rejected") - else - println("❌ Test 1 FAILED: Wrong error message: $(e)") - end -end - -# Test case 2: Scalar integration should still work -try - result = integrate(x) - println("✅ Test 2 PASSED: Scalar integration works: $result") -catch e - println("❌ Test 2 FAILED: Scalar integration should work but got: $(e)") -end - -println("\nAll tests completed!") \ No newline at end of file diff --git a/test_vector_fix.jl b/test_vector_fix.jl deleted file mode 100644 index 593aa67..0000000 --- a/test_vector_fix.jl +++ /dev/null @@ -1,47 +0,0 @@ -using Pkg -Pkg.activate(".") -using Symbolics, SymbolicNumericIntegration - -println("Testing vector expression error handling:") -println() - -# Test case from issue #58 -@variables α -exp2 = [1, 2 * α] - -println("Test 1: integrate([1, 2α], α)") -try - result = integrate(exp2, α) - println("ERROR: Should have thrown an error but got: $result") -catch e - println("✓ Correctly caught error: $(e)") -end - -println() -println("Test 2: Verify element-wise integration still works") -try - result = integrate.(exp2, α) - println("✓ Element-wise integration works: $result") -catch e - println("ERROR: Element-wise should work but got: $(e)") -end - -println() -# Test case from issue #106 -@variables x -println("Test 3: integrate([x])") -try - result = integrate([x]) - println("ERROR: Should have thrown an error but got: $result") -catch e - println("✓ Correctly caught error: $(e)") -end - -println() -println("Test 4: Verify scalar integration still works") -try - result = integrate(2 * α, α) - println("✓ Scalar integration works: $result") -catch e - println("ERROR: Scalar should work but got: $(e)") -end \ No newline at end of file