clear;
clc;
format long;

% 2 Fisher Matrixes symbolic : FISH_GCsp_SYM, : 1 cosmo params + 1 bias spectro put for common
%                              FISH_XC_SYM : 1 cosmo params + 2 bias photo correlated

% Spectro

% FISH_SYM2 : from big Fisher matrix
FISH_GCsp_SYM = sym('sp1_', [4,4], 'real');
FISH_GCsp_SYM(3:4,3:4) = 0;
FISH_GCsp_SYM(1:2,3:4) = 0;
FISH_GCsp_SYM(3:4,1:2) = 0;
% Force symmetry
FISH_GCsp_SYM = tril(FISH_GCsp_SYM.') + triu(FISH_GCsp_SYM,1)

% First parameter : cosmo , second : 2 bias photo correlated
FISH_XC_SYM = sym('xc1_', [4,4], 'real');
%FISH_XC_ADD2 = sym('eps_xc_', [3,3], 'real');
FISH_XC_SYM(1,2) = 0;
FISH_XC_SYM(2,1) = 0;
FISH_XC_SYM(2,2) = 0;
FISH_XC_SYM = tril(FISH_XC_SYM.') + triu(FISH_XC_SYM,1)

% FISH_ALL1 : brutal marginalization on Fisher matrix
FISH_ALL1(1:2,1:2) = FISH_GCsp_SYM(1:2,1:2) + FISH_XC_SYM(1:2:3,1:2:3);

% Adding new observable "O" terms
FISH_O_SYM = sym('o_', [2,2], 'real');
SIGMA_O = sym('sigma_o', 'real');
FISH_O_SYM = 1/(SIGMA_O*SIGMA_O) * (tril(FISH_O_SYM.') + triu(FISH_O_SYM,1));
%FISH_O_SYM = (tril(FISH_O_SYM.') + triu(FISH_O_SYM,1))

% Multiply by a numerical unknown sigma_o
%syms sigma_o
%sigma_o = 1e-3
%FISH_O_SYM = 1./sigma_o^2*FISH_O_SYM

% Addition of second Fisher matrix
FISH_SYM2 = FISH_GCsp_SYM + FISH_XC_SYM;

FISH_SYM2(2,2) = FISH_SYM2(2,2) + FISH_O_SYM(1,1);
FISH_SYM2(3,3) = FISH_SYM2(3,3) + FISH_O_SYM(2,2);
FISH_SYM2(2,3) = FISH_SYM2(2,3) + FISH_O_SYM(1,2);
FISH_SYM2(3,2) = FISH_SYM2(3,2) + FISH_O_SYM(2,1);

% Marginalize FISH_SYM2 in order to get back a 2x2 matrix

% Invert to marginalyze
COV_ALL = inv(FISH_SYM2);
% Marginalize
COV_ALL([3,4],:) = [];
COV_ALL(:,[3,4]) = [];
FISH_ALL2 = inv(COV_ALL);

FISH_ALL1;
FISH_ALL2;

% Matricial equation to solve
eqn = FISH_ALL1 == FISH_ALL2;

% Solving : matrix1 equal to matrix2
%sol = solve(eqn, [FISH_O_SYM(1,1),FISH_O_SYM(2,2),FISH_O_SYM(1,2)])
sol = solve(eqn, [FISH_O_SYM(1,1),FISH_O_SYM(2,2),FISH_O_SYM(1,2),SIGMA_O])

