Skip to content

Manipulating matrices and vectors of polyvar #139

@richardrl

Description

@richardrl

Hi,
Thanks for the great library!

I'm wondering how I can operate on matrices or vectors of polynomials. I have tried this:

@polyvar rot_mat_flat_var_original[1:num_internal_bodies, 6]

creating what (I think) should be a (num_internal_bodies, 6) shaped array, but it does not function like matrix or vector.

For example, I cannot call any length or size after indexing into it and getting a new 'vector' out.

More concretely, here is something that works, and something that doesn't.
Works:

function convertlowertri_tomat(r)
    n = Int((-1 + sqrt(1 + 8 * length(r)))/2)
    # outmat = zeros(n, n)
    outmat = Matrix{Any}(undef, n, n)
    idx = 1
    for i in 1:n
        for j in 1:i
            outmat[i, j] = r[idx]
            outmat'[i, j] = r[idx]

            idx += 1
        end
    end
    return outmat
end

@polyvar r1[1:6]

convertlowertri_tomat(r1)

The above code successfully converts an array containing lower triangular components, described as: PolyVar{true}[r1₁, r1₂, r1₃, r1₄, r1₅, r1₆], into a symmetric matrix.

However, suppose I am storing a bunch of such arrays into this matrix, of shape (n x 6) instead of shape (6,) as before.

Indexing into this matrix does not function as expected. Indexing into this matrix, I would assume, gives me a vector of shape (1, 6) or (6,) that I can input into convertlowertri_tomat(...), but that is not the case.

@polyvar rot_mat_flat_var_original[1:num_internal_bodies, 6]
convertlowertri_tomat(rot_mat_flat_var_original[1])

the above code errors with:
ERROR: LoadError: MethodError: no method matching length(::PolyVar{true})

meaning indexing into the "matrix" does not give me a "vector".

In my mind, a @PolyVar defined with extra dimensions can be treated just like a tensor, to be indexed at will, but this seems not the case.

Any clue how I can fix my code so that I can store a matrix of polyvars, index into its first dimension, and have it work with convertlowertri_tomat?

Additionally, when I print out rot_mat_flat_var_original[1], it gives this: PolyVar{true} which does not look like the vector data type PolyVar{true}[r1₁, r1₂, r1₃, r1₄, r1₅, r1₆] from before.

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