SymPy.jl

When functions are linear in parameters, we can decompose a matrix of polynomials into a matrix of coefficients and a matrix of variables. To achieve this, we can use the SymPy package.

First we need to ensure that Julia recognizes variables and treats them as symbols.

using SymPy 
x, y = symbols("x, y")

Then we can simply invoke thecoeff() method to extract coefficients from the polynomials.

p = x + 0.5*y + 1 

p.coeff(x)
p.coeff(y)

Output:

1
0.500000000000000

Next, suppose we have matrix of polynomials, \(P(x;\theta)\) denoted by Pm below in Julia. Note that each element of Pm is linear in the parameters. Thus, we can represent Pm as the multiplication of a matrix of coefficients, \(C(\theta)\) denoted by C in Julia, and a vector of variables \( \mathbf{x}\), [x,y].

$$ P(x;\theta) = C(\theta) \cdot \mathbf{x} $$

Pm= [2*x+ 3*y; 1*x+5*y]
# number of polynomials, M
M=length(Pm)
# number of variables, L
L=2 
# create C, coefficient matrix
C=zeros(L, M)
for i in 1:M
    C[i, 1]=p[i].coeff(x)
    C[i, 2]=p[i].coeff(y) 
end 
C

Output:

2×2 Matrix{Float64}:
 2.0  3.0
 1.0  5.0

Notice that multiplying C by [x, y] is equivalent to Pm.

C*[x,  y]

Output:

2-element Vector{Sym}:
 2.0⋅x + 3.0⋅y
 1.0⋅x + 5.0⋅y