Search code examples
matlabmathmatrixlinear-algebrasymbolic-math

easiest way to prototype a symbolic orthogonal matrix


I have 8 sins and cosines that I try to symbolically define as shown below using Matlab. My goal is to symbolically build a matrix H (accumulated Givens rotations matrix) of 8x8 using all these sins and cosines and end up seeing what the formula for this H orthogonal projection matrix is. I can do that using the code below conceptually G7*G6*...*G0*I where I is the Identity 8x8 and the Gi are the Givens rotation corresponding to elements (i:i+1,i:i+1).

c_0 = sym('c_0');
c_1 = sym('c_1');
c_2 = sym('c_2');
c_3 = sym('c_3');
c_4 = sym('c_4');
c_5 = sym('c_5');
c_6 = sym('c_6');
c_7 = sym('c_7');

s_0 = sym('s_0');
s_1 = sym('s_1');
s_2 = sym('s_2');
s_3 = sym('s_3');
s_4 = sym('s_4');
s_5 = sym('s_5');
s_6 = sym('s_6');
s_7 = sym('s_7');

% create H orthogonal matrix using the sin and cos symbols
% filling in the first rotation
I=eye(9,9)
H = I;
H(1:2,1:2) = [c_0 -s_0; s_0 c_0]

% build the 2nd rotation and update H
G = I;
G(2:3,2:3) = [c_1 -s_1; s_1 c_1]
H = G*H

% build the 3rd rotation and update H
G = I;
G(3:4,3:4) = [c_2 -s_2; s_2 c_2]
H = G*H

% build the 4rth rotation and update H
G = I;
G(4:5,4:5) = [c_3 -s_3; s_3 c_3]
H = G*H

% build the 5th rotation and update H
G = I;
G(5:6,5:6) = [c_4 -s_4; s_4 c_4]
H = G*H

% build the 6th rotation and update H
G = I;
G(6:7,6:7) = [c_5 -s_5; s_5 c_5]
H = G*H

% build the 7th rotation and update H
G = I;
G(7:8,7:8) = [c_6 -s_6; s_6 c_6]
H = G*H

% build the 8th rotation and update H
G = I;
G(8:9,8:9) = [c_7 -s_7; s_7 c_7]
H = G*H

The code fails with the following error and can't find how to fix this:

The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

Error in build_rotH_test (line 26)
H(1:2,1:2) = [c_0 -s_0; s_0 c_0]

Solution

  • I solved it like this. Note I realized I need the transpose of each rotation so I can build and apply H'*x i.e. G7'*G6'*...*G0'*I that's why the sin signs are flipped in the solution.

    clear all;
    
    % defining 0 and 1 as symbols too, solves the problem
    sym_0 = sym('0');
    sym_1 = sym('1');
    
    c0 = sym('c0');
    c1 = sym('c1');
    c2 = sym('c2');
    c3 = sym('c3');
    c4 = sym('c4');
    c5 = sym('c5');
    c6 = sym('c6');
    c7 = sym('c7');
    
    s0 = sym('s0');
    s1 = sym('s1');
    s2 = sym('s2');
    s3 = sym('s3');
    s4 = sym('s4');
    s5 = sym('s5');
    s6 = sym('s6');
    s7 = sym('s7');
    
    % create H orthogonal matrix using the sin and cos symbols
    % filling in the first rotation
    I = repmat(sym_0,9,9);
    for i=1:9
        I(i,i)=sym_1;
    end
    H = I
    H(1:2,1:2) = [c0 s0; -s0 c0]
    
    % build the 2nd rotation and update H
    G = I;
    G(2:3,2:3) = [c1 s1; -s1 c1]
    H = G*H;
    
    % build the 3rd rotation and update H
    G = I;
    G(3:4,3:4) = [c2 s2; -s2 c2]
    H = G*H;
    
    % build the 4rth rotation and update H
    G = I;
    G(4:5,4:5) = [c3 s3; -s3 c3]
    H = G*H;
    
    % build the 5th rotation and update H
    G = I;
    G(5:6,5:6) = [c4 s4; -s4 c4]
    H = G*H;
    
    % build the 6th rotation and update H
    G = I;
    G(6:7,6:7) = [c5 s5; -s5 c5]
    H = G*H;
    
    % build the 7th rotation and update H
    G = I;
    G(7:8,7:8) = [c6 s6; -s6 c6]
    H = G*H;
    
    % build the 8th rotation and update H
    G = I;
    G(8:9,8:9) = [c7 s7; -s7 c7]
    H = G*H
    

    and the output is:

    H =
    
    [ 1, 0, 0, 0, 0, 0, 0, 0, 0]
    [ 0, 1, 0, 0, 0, 0, 0, 0, 0]
    [ 0, 0, 1, 0, 0, 0, 0, 0, 0]
    [ 0, 0, 0, 1, 0, 0, 0, 0, 0]
    [ 0, 0, 0, 0, 1, 0, 0, 0, 0]
    [ 0, 0, 0, 0, 0, 1, 0, 0, 0]
    [ 0, 0, 0, 0, 0, 0, 1, 0, 0]
    [ 0, 0, 0, 0, 0, 0, 0, 1, 0]
    [ 0, 0, 0, 0, 0, 0, 0, 0, 1]
    
    
    H =
    
    [  c0, s0, 0, 0, 0, 0, 0, 0, 0]
    [ -s0, c0, 0, 0, 0, 0, 0, 0, 0]
    [   0,  0, 1, 0, 0, 0, 0, 0, 0]
    [   0,  0, 0, 1, 0, 0, 0, 0, 0]
    [   0,  0, 0, 0, 1, 0, 0, 0, 0]
    [   0,  0, 0, 0, 0, 1, 0, 0, 0]
    [   0,  0, 0, 0, 0, 0, 1, 0, 0]
    [   0,  0, 0, 0, 0, 0, 0, 1, 0]
    [   0,  0, 0, 0, 0, 0, 0, 0, 1]
    
    
    G =
    
    [ 1,   0,  0, 0, 0, 0, 0, 0, 0]
    [ 0,  c1, s1, 0, 0, 0, 0, 0, 0]
    [ 0, -s1, c1, 0, 0, 0, 0, 0, 0]
    [ 0,   0,  0, 1, 0, 0, 0, 0, 0]
    [ 0,   0,  0, 0, 1, 0, 0, 0, 0]
    [ 0,   0,  0, 0, 0, 1, 0, 0, 0]
    [ 0,   0,  0, 0, 0, 0, 1, 0, 0]
    [ 0,   0,  0, 0, 0, 0, 0, 1, 0]
    [ 0,   0,  0, 0, 0, 0, 0, 0, 1]
    
    
    G =
    
    [ 1, 0,   0,  0, 0, 0, 0, 0, 0]
    [ 0, 1,   0,  0, 0, 0, 0, 0, 0]
    [ 0, 0,  c2, s2, 0, 0, 0, 0, 0]
    [ 0, 0, -s2, c2, 0, 0, 0, 0, 0]
    [ 0, 0,   0,  0, 1, 0, 0, 0, 0]
    [ 0, 0,   0,  0, 0, 1, 0, 0, 0]
    [ 0, 0,   0,  0, 0, 0, 1, 0, 0]
    [ 0, 0,   0,  0, 0, 0, 0, 1, 0]
    [ 0, 0,   0,  0, 0, 0, 0, 0, 1]
    
    
    G =
    
    [ 1, 0, 0,   0,  0, 0, 0, 0, 0]
    [ 0, 1, 0,   0,  0, 0, 0, 0, 0]
    [ 0, 0, 1,   0,  0, 0, 0, 0, 0]
    [ 0, 0, 0,  c3, s3, 0, 0, 0, 0]
    [ 0, 0, 0, -s3, c3, 0, 0, 0, 0]
    [ 0, 0, 0,   0,  0, 1, 0, 0, 0]
    [ 0, 0, 0,   0,  0, 0, 1, 0, 0]
    [ 0, 0, 0,   0,  0, 0, 0, 1, 0]
    [ 0, 0, 0,   0,  0, 0, 0, 0, 1]
    
    
    G =
    
    [ 1, 0, 0, 0,   0,  0, 0, 0, 0]
    [ 0, 1, 0, 0,   0,  0, 0, 0, 0]
    [ 0, 0, 1, 0,   0,  0, 0, 0, 0]
    [ 0, 0, 0, 1,   0,  0, 0, 0, 0]
    [ 0, 0, 0, 0,  c4, s4, 0, 0, 0]
    [ 0, 0, 0, 0, -s4, c4, 0, 0, 0]
    [ 0, 0, 0, 0,   0,  0, 1, 0, 0]
    [ 0, 0, 0, 0,   0,  0, 0, 1, 0]
    [ 0, 0, 0, 0,   0,  0, 0, 0, 1]
    
    
    G =
    
    [ 1, 0, 0, 0, 0,   0,  0, 0, 0]
    [ 0, 1, 0, 0, 0,   0,  0, 0, 0]
    [ 0, 0, 1, 0, 0,   0,  0, 0, 0]
    [ 0, 0, 0, 1, 0,   0,  0, 0, 0]
    [ 0, 0, 0, 0, 1,   0,  0, 0, 0]
    [ 0, 0, 0, 0, 0,  c5, s5, 0, 0]
    [ 0, 0, 0, 0, 0, -s5, c5, 0, 0]
    [ 0, 0, 0, 0, 0,   0,  0, 1, 0]
    [ 0, 0, 0, 0, 0,   0,  0, 0, 1]
    
    
    G =
    
    [ 1, 0, 0, 0, 0, 0,   0,  0, 0]
    [ 0, 1, 0, 0, 0, 0,   0,  0, 0]
    [ 0, 0, 1, 0, 0, 0,   0,  0, 0]
    [ 0, 0, 0, 1, 0, 0,   0,  0, 0]
    [ 0, 0, 0, 0, 1, 0,   0,  0, 0]
    [ 0, 0, 0, 0, 0, 1,   0,  0, 0]
    [ 0, 0, 0, 0, 0, 0,  c6, s6, 0]
    [ 0, 0, 0, 0, 0, 0, -s6, c6, 0]
    [ 0, 0, 0, 0, 0, 0,   0,  0, 1]
    
    
    G =
    
    [ 1, 0, 0, 0, 0, 0, 0,   0,  0]
    [ 0, 1, 0, 0, 0, 0, 0,   0,  0]
    [ 0, 0, 1, 0, 0, 0, 0,   0,  0]
    [ 0, 0, 0, 1, 0, 0, 0,   0,  0]
    [ 0, 0, 0, 0, 1, 0, 0,   0,  0]
    [ 0, 0, 0, 0, 0, 1, 0,   0,  0]
    [ 0, 0, 0, 0, 0, 0, 1,   0,  0]
    [ 0, 0, 0, 0, 0, 0, 0,  c7, s7]
    [ 0, 0, 0, 0, 0, 0, 0, -s7, c7]
    
    
    H =
    
    [                       c0,                       s0,                     0,                  0,               0,            0,         0,      0,  0]
    [                   -c1*s0,                    c0*c1,                    s1,                  0,               0,            0,         0,      0,  0]
    [                 c2*s0*s1,                -c0*c2*s1,                 c1*c2,                 s2,               0,            0,         0,      0,  0]
    [             -c3*s0*s1*s2,              c0*c3*s1*s2,             -c1*c3*s2,              c2*c3,              s3,            0,         0,      0,  0]
    [           c4*s0*s1*s2*s3,          -c0*c4*s1*s2*s3,           c1*c4*s2*s3,          -c2*c4*s3,           c3*c4,           s4,         0,      0,  0]
    [       -c5*s0*s1*s2*s3*s4,        c0*c5*s1*s2*s3*s4,       -c1*c5*s2*s3*s4,        c2*c5*s3*s4,       -c3*c5*s4,        c4*c5,        s5,      0,  0]
    [     c6*s0*s1*s2*s3*s4*s5,    -c0*c6*s1*s2*s3*s4*s5,     c1*c6*s2*s3*s4*s5,    -c2*c6*s3*s4*s5,     c3*c6*s4*s5,    -c4*c6*s5,     c5*c6,     s6,  0]
    [ -c7*s0*s1*s2*s3*s4*s5*s6,  c0*c7*s1*s2*s3*s4*s5*s6, -c1*c7*s2*s3*s4*s5*s6,  c2*c7*s3*s4*s5*s6, -c3*c7*s4*s5*s6,  c4*c7*s5*s6, -c5*c7*s6,  c6*c7, s7]
    [  s0*s1*s2*s3*s4*s5*s6*s7, -c0*s1*s2*s3*s4*s5*s6*s7,  c1*s2*s3*s4*s5*s6*s7, -c2*s3*s4*s5*s6*s7,  c3*s4*s5*s6*s7, -c4*s5*s6*s7,  c5*s6*s7, -c6*s7, c7]