gnu-octave / symbolic

A Symbolic Package for Octave using SymPy
https://octave.sourceforge.io/symbolic/
GNU General Public License v3.0
151 stars 36 forks source link

Kronecker product (tensor product) does not work for three inputs if one (or more) inputs involve symbols #1245

Closed michaelaoash closed 1 year ago

michaelaoash commented 1 year ago

The following code illustrates the problem.

## Problem: kronecker product (tensor product) does not work for three
## inputs if one (or more) inputs involve symbols from package symbolic

pkg load symbolic

## N_sqrt2 will stand in for 1/sqrt(2)
N_sqrt2 = sym('N_sqrt2')

## Hadamard matrix (numeric and symbolic versions)
H_numeric = 1/sqrt(2) * [1 1 ; 1 -1]
H_symbolic = N_sqrt2 * [1 1 ; 1 -1]

## kronecker product (tensor product) works fine for symbolic with two inputs
kron(H_numeric, eye(2))
kron(H_symbolic, eye(2))

## kronecker product (tensor product) works fine for three numeric
## inputs but does not work for three inputs when at least one is
## symbolic
kron(H_numeric, eye(2), eye(2))
kron(H_symbolic, eye(2), eye(2))

## Problem can be finessed by taking explicit advantage of the associative
## property, that is, use two two-input kronecker products
kron(kron(H_symbolic, eye(2)), eye(2))
kron(H_symbolic, kron(eye(2), eye(2)))
cbm755 commented 1 year ago

Confirmed! should be a easy fix using the associative property you mention.

cbm755 commented 1 year ago

Fixed in #1246.