%% input
clc; clear all;
% J = 5;
% cell_rhs_i = {[2 2 3 3]}; %set of 4 (j1, m1, j2, m2), %ij1 = 1, im1 = 2, ij2 = 3, im2 = 4
% cell_rhs_c = {sym([1])}; %const matrix
% J = 4;
% cell_rhs_i = {[2 2 3 2; 2 1 3 3]}; %set of 4 (j1, m1, j2, m2), %ij1 = 1, im1 = 2, ij2 = 3, im2 = 4
% cell_rhs_c = {sym([sqrt(2/5), -sqrt(3/5)])}; %const matrix
% J = 3;
% cell_rhs_i = {[2 2 3 1; 2 1 3 2; 2 0 3 3]}; %set of 4 (j1, m1, j2, m2), %ij1 = 1, im1 = 2, ij2 = 3, im2 = 4
% cell_rhs_c = {sym([sqrt(1/6), -sqrt(5/12), sqrt(5/12)])}; %const matrix
% J = 2;
% cell_rhs_i = {[2 2 3 0; 2 1 3 1; 2 0 3 2; 2 -1 3 3]}; %set of 4 (j1, m1, j2, m2), %ij1 = 1, im1 = 2, ij2 = 3, im2 = 4
% cell_rhs_c = {sym([sqrt(1/14), -sqrt(3/14), sqrt(5/14), -sqrt(5/14)])}; %const matrix
J = 1;
cell_rhs_i = {[2 2 3 -1; 2 1 3 0; 2 0 3 1; 2 -1 3 2; 2 -2 3 3]}; %set of 4 (j1, m1, j2, m2), %ij1 = 1, im1 = 2, ij2 = 3, im2 = 4
cell_rhs_c = {sym([sqrt(1/35), -sqrt(3/35), sqrt(6/35), -sqrt(10/35), sqrt(15/35)])};
%% loop
Ms = (J:-1:-J);
for r = 2:numel(Ms)
prevM = Ms(r-1);
prev_rhs_i = cell_rhs_i{r-1};
prev_rhs_c = cell_rhs_c{r-1};
[numprevtouble, temp] = size(prev_rhs_i); %temp will be 4
curr_rhs_i = [];
curr_rhs_c = [];
% prev_rhs_i
% prev_rhs_c
for c=1:numprevtouble
[children_i children_c] = op_jminus(prev_rhs_i(c,:), prev_rhs_c(c));
% [children_i children_c]
for ichild=1:numel(children_c)
achild_i = children_i(ichild,:);
achild_c = children_c(ichild);
iloc = locateindex(curr_rhs_i, achild_i);
if (iloc == 0)
curr_rhs_i(end+1,:) = achild_i;
curr_rhs_c(end+1) = achild_c;
else
curr_rhs_c(iloc) = curr_rhs_c(iloc) + achild_c;
end
end
end
curr_rhs_c = curr_rhs_c / coef_jminus(J, prevM);
curr_rhs_c = simple(curr_rhs_c);
cell_rhs_i = {cell_rhs_i{:} curr_rhs_i};
cell_rhs_c = {cell_rhs_c{:} curr_rhs_c};
r = r + 1;
end
%% display content of the cell
clc
for i = 1:numel(Ms)
disp(['J=' num2str(J), ',M=' num2str(Ms(i))]);
[cell_rhs_i{i}, cell_rhs_c{i}']
end