Skip to content

-array_expr fails with "Matrices are the only arrays that can be raised to a power" #1772

@1-Bart-1

Description

@1-Bart-1

When negating a symbolic array expression that is a sum of scalar * array terms, the unary negation operator produces an invalid result that cannot be scalarized. The expression incorrectly collapses to -1 - 1 instead of remaining a 3-element array.

Interestingly, -1 * array_expr works correctly - only the unary - operator is broken.

MWE:

using Symbolics
using LinearAlgebra: ×

@variables t
@variables group_chord(t)[1:3, 1:4]
@variables group_y_airf(t)[1:3, 1:4]
@variables twist_angle(t)[1:4]

# Build symbolic vectors
gc = collect(group_chord[:, 1])
gc_norm = sqrt(sum(abs2.(gc)))
x_airf = gc / gc_norm

gy = collect(group_y_airf[:, 1])
init_z_airf = x_airf × gy

# Sum of scalar * array terms - this is a valid 3-element symbolic array
z_airf = sin(twist_angle[1]) * x_airf + cos(twist_angle[1]) * init_z_airf

# Scalarizing z_airf works fine
println("scalarize(z_airf) works:")
println(Symbolics.scalarize(z_airf))

# Workaround: -1*z_airf works
println("\nscalarize(-1*z_airf) works:")
println(Symbolics.scalarize(-1*z_airf))

# BUG: Scalarizing -z_airf (unary negation) fails
# First, look at what -z_airf looks like (use show to avoid scalarization in print)
print("\n-z_airf = "); show(stdout, -z_airf); println()
# Note: It shows as "-1 - 1" which is bizarre - a 3-element array became scalar -2!

println("\nscalarize(-z_airf) fails:")
Symbolics.scalarize(-z_airf)

Error:

ArgumentError: Matrices are the only arrays that can be raised to a power. Found array of shape UnitRange{Int64}[1:3].

Observed behavior:

  • show(stdout, -z_airf) displays -1 - 1 instead of a 3-element array
  • scalarize(-z_airf) throws the power error

Expected behavior:

  • -z_airf should be a 3-element symbolic array (same as -1 * z_airf)
  • scalarize(-z_airf) should work and return element-wise negation

Workaround:

Use -1 * z_airf instead of -z_airf.

Version info:

  • Julia: v1.12.4
  • Symbolics.jl: [0c5d862f] Symbolics v7.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions