Skip to content

Conversation

Copy link

Copilot AI commented Jan 15, 2026

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Fix π Parsing in Complex Values for PR #3

Problem

The PR #3 has test failures like:

FAILED array_api_tests/test_special_cases.py::test_unary[acosh((real(x_i) is +0 or real(x_i) == -0) and imag(x_i) is +0) -> +0 + πj/2] - AssertionError: out=1.5707963267948966j, but should be +0 + πj/2 [acosh()]

The actual output 1.5707963267948966j is numerically correct (it equals π/2), but the test is failing because parse_complex_value() in array_api_tests/test_special_cases.py (lines 495-523) doesn't correctly handle mathematical expressions like πj/2 in complex values.

Root Cause

The issue is in the parse_complex_value() function and r_complex_value regex (line 697-699):

  1. The regex r_complex_value = re.compile(r"([+-]?)([^\s]+)\s*([+-])\s*([^\s]+)\s*j") captures the components but doesn't handle expressions like πj/2 which have a division
  2. The parse_complex_value() function directly passes captured strings to parse_value(), but for πj/2 it captures π as the imaginary part string, missing the /2 part

Solution Required

Update the regex and parsing logic in array_api_tests/test_special_cases.py to:

  1. Update r_complex_value regex (line 697-699) to properly capture complex numbers with mathematical expressions in the imaginary part, e.g.:

    • +0 + πj/2 should capture real=+0, imag=π/2
    • +0 + 0j should capture real=+0, imag=0
    • NaN + NaN j should capture real=NaN, imag=NaN
  2. The regex should handle:

    • Optional spaces around operators
    • Mathematical expressions with / for division
    • The j can be attached or have a space before it
  3. The key insight: The existing parse_value() function (lines 138-167) already handles π expressions correctly including π/2. We just need to properly extract and pass the imaginary coefficient to it.

Suggested Fix

Replace the r_complex_value regex around line 697 with:

r_complex_value = re.compile(
    r"([+-]?)([^\s]+)\s*([+-])\s*([\dπ/]+)\s*j"
)

This allows capturing digits, π, and / in the imaginary part, which can then be passed to parse_value() which already knows how to evaluate π/2.

Test Cases to Verify

After the fix, these should parse correctly:

  • +0 + πj/2complex(0.0, 1.5707963267948966)
  • +0 + 0jcomplex(0.0, 0.0)
  • NaN + NaN jcomplex(nan, nan)

Files to Modify

  • array_api_tests/test_special_cases.py - Update the r_complex_value regex pattern (around line 697-699)

The existing parse_complex_value() function should work correctly once the regex properly captures the full imaginary expression.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@ev-br ev-br closed this Jan 15, 2026
Copilot AI requested a review from ev-br January 15, 2026 20:43
Copilot stopped work on behalf of ev-br due to an error January 15, 2026 20:43
@ev-br
Copy link
Owner

ev-br commented Jan 15, 2026

this should have been a part of #3, so continuing there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants