%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%                                                                  %%%%%
%%%%    IEEE PES Power Grid Library - Optimal Power Flow - v21.07     %%%%%
%%%%          (https://github.com/power-grid-lib/pglib-opf)           %%%%%
%%%%             Benchmark Group - Active Power Increase              %%%%%
%%%%                         29 - July - 2021                         %%%%%
%%%%                                                                  %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mpc = pglib_opf_case30_ieee__api
mpc.version = '2';
mpc.baseMVA = 100.0;

%% bus data
%	bus_i	type	Pd	Qd	Gs	Bs	area	Vm	Va	baseKV	zone	Vmax	Vmin
mpc.bus = [
	1	 3	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	2	 2	 36.08	 12.70	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	3	 1	 3.99	 1.20	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	4	 1	 12.64	 1.60	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	5	 2	 156.64	 19.00	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	6	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	7	 1	 37.91	 10.90	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	8	 2	 49.89	 30.00	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	9	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 1.0	 1	    1.06000	    0.94000;
	10	 1	 9.64	 2.00	 0.0	 19.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	11	 2	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 11.0	 1	    1.06000	    0.94000;
	12	 1	 18.62	 7.50	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	13	 2	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 11.0	 1	    1.06000	    0.94000;
	14	 1	 10.31	 1.60	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	15	 1	 13.64	 2.50	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	16	 1	 5.82	 1.80	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	17	 1	 14.97	 5.80	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	18	 1	 5.32	 0.90	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	19	 1	 15.80	 3.40	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	20	 1	 3.66	 0.70	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	21	 1	 29.10	 11.20	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	22	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	23	 1	 5.32	 1.60	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	24	 1	 14.47	 6.70	 0.0	 4.3	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	25	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	26	 1	 5.82	 2.30	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	27	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	28	 1	 0.0	 0.0	 0.0	 0.0	 1	    1.00000	    0.00000	 132.0	 1	    1.06000	    0.94000;
	29	 1	 3.99	 0.90	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
	30	 1	 17.63	 1.90	 0.0	 0.0	 1	    1.00000	    0.00000	 33.0	 1	    1.06000	    0.94000;
];

%% generator data
%	bus	Pg	Qg	Qmax	Qmin	Vg	mBase	status	Pmax	Pmin
mpc.gen = [
	1	 175.5	 0.0	 176.0	 -176.0	 1.0	 100.0	 1	 351	 0.0; % NG
	2	 171.0	 0.0	 171.0	 -171.0	 1.0	 100.0	 1	 342	 0.0; % NG
	5	 0.0	 0.0	 40.0	 -40.0	 1.0	 100.0	 1	 0	 0.0; % SYNC
	8	 0.0	 0.0	 230.4	 -230.4	 1.0	 100.0	 1	 0	 0.0; % SYNC
	11	 0.0	 3.0	 24.0	 -18.0	 1.0	 100.0	 1	 0	 0.0; % SYNC
	13	 0.0	 9.0	 24.0	 -6.0	 1.0	 100.0	 1	 0	 0.0; % SYNC
];

%% generator cost data
%	2	startup	shutdown	n	c(n-1)	...	c0
mpc.gencost = [
	2	 0.0	 0.0	 3	   0.000000	  18.421528	   0.000000; % NG
	2	 0.0	 0.0	 3	   0.000000	  52.182254	   0.000000; % NG
	2	 0.0	 0.0	 3	   0.000000	   0.000000	   0.000000; % SYNC
	2	 0.0	 0.0	 3	   0.000000	   0.000000	   0.000000; % SYNC
	2	 0.0	 0.0	 3	   0.000000	   0.000000	   0.000000; % SYNC
	2	 0.0	 0.0	 3	   0.000000	   0.000000	   0.000000; % SYNC
];

%% branch data
%	fbus	tbus	r	x	b	rateA	rateB	rateC	ratio	angle	status	angmin	angmax
mpc.branch = [
	1	 2	 0.0192	 0.0575	 0.0528	 138.0	 138.0	 138.0	 0.0	 0.0	 1	 -30.0	 30.0;
	1	 3	 0.0452	 0.1652	 0.0408	 152.0	 152.0	 152.0	 0.0	 0.0	 1	 -30.0	 30.0;
	2	 4	 0.057	 0.1737	 0.0368	 139.0	 139.0	 139.0	 0.0	 0.0	 1	 -30.0	 30.0;
	3	 4	 0.0132	 0.0379	 0.0084	 135.0	 135.0	 135.0	 0.0	 0.0	 1	 -30.0	 30.0;
	2	 5	 0.0472	 0.1983	 0.0418	 144.0	 144.0	 144.0	 0.0	 0.0	 1	 -30.0	 30.0;
	2	 6	 0.0581	 0.1763	 0.0374	 139.0	 139.0	 139.0	 0.0	 0.0	 1	 -30.0	 30.0;
	4	 6	 0.0119	 0.0414	 0.009	 148.0	 148.0	 148.0	 0.0	 0.0	 1	 -30.0	 30.0;
	5	 7	 0.046	 0.116	 0.0204	 127.0	 127.0	 127.0	 0.0	 0.0	 1	 -30.0	 30.0;
	6	 7	 0.0267	 0.082	 0.017	 140.0	 140.0	 140.0	 0.0	 0.0	 1	 -30.0	 30.0;
	6	 8	 0.012	 0.042	 0.009	 148.0	 148.0	 148.0	 0.0	 0.0	 1	 -30.0	 30.0;
	6	 9	 0.0	 0.208	 0.0	 142.0	 142.0	 142.0	 0.978	 0.0	 1	 -30.0	 30.0;
	6	 10	 0.0	 0.556	 0.0	 53.0	 53.0	 53.0	 0.969	 0.0	 1	 -30.0	 30.0;
	9	 11	 0.0	 0.208	 0.0	 142.0	 142.0	 142.0	 1.0	 0.0	 1	 -30.0	 30.0;
	9	 10	 0.0	 0.11	 0.0	 267.0	 267.0	 267.0	 1.0	 0.0	 1	 -30.0	 30.0;
	4	 12	 0.0	 0.256	 0.0	 115.0	 115.0	 115.0	 0.932	 0.0	 1	 -30.0	 30.0;
	12	 13	 0.0	 0.14	 0.0	 210.0	 210.0	 210.0	 1.0	 0.0	 1	 -30.0	 30.0;
	12	 14	 0.1231	 0.2559	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	12	 15	 0.0662	 0.1304	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	12	 16	 0.0945	 0.1987	 0.0	 30.0	 30.0	 30.0	 0.0	 0.0	 1	 -30.0	 30.0;
	14	 15	 0.221	 0.1997	 0.0	 20.0	 20.0	 20.0	 0.0	 0.0	 1	 -30.0	 30.0;
	16	 17	 0.0524	 0.1923	 0.0	 38.0	 38.0	 38.0	 0.0	 0.0	 1	 -30.0	 30.0;
	15	 18	 0.1073	 0.2185	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	18	 19	 0.0639	 0.1292	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	19	 20	 0.034	 0.068	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	10	 20	 0.0936	 0.209	 0.0	 30.0	 30.0	 30.0	 0.0	 0.0	 1	 -30.0	 30.0;
	10	 17	 0.0324	 0.0845	 0.0	 33.0	 33.0	 33.0	 0.0	 0.0	 1	 -30.0	 30.0;
	10	 21	 0.0348	 0.0749	 0.0	 30.0	 30.0	 30.0	 0.0	 0.0	 1	 -30.0	 30.0;
	10	 22	 0.0727	 0.1499	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	21	 22	 0.0116	 0.0236	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	15	 23	 0.1	 0.202	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	22	 24	 0.115	 0.179	 0.0	 26.0	 26.0	 26.0	 0.0	 0.0	 1	 -30.0	 30.0;
	23	 24	 0.132	 0.27	 0.0	 29.0	 29.0	 29.0	 0.0	 0.0	 1	 -30.0	 30.0;
	24	 25	 0.1885	 0.3292	 0.0	 27.0	 27.0	 27.0	 0.0	 0.0	 1	 -30.0	 30.0;
	25	 26	 0.2544	 0.38	 0.0	 25.0	 25.0	 25.0	 0.0	 0.0	 1	 -30.0	 30.0;
	25	 27	 0.1093	 0.2087	 0.0	 28.0	 28.0	 28.0	 0.0	 0.0	 1	 -30.0	 30.0;
	28	 27	 0.0	 0.396	 0.0	 75.0	 75.0	 75.0	 0.968	 0.0	 1	 -30.0	 30.0;
	27	 29	 0.2198	 0.4153	 0.0	 28.0	 28.0	 28.0	 0.0	 0.0	 1	 -30.0	 30.0;
	27	 30	 0.3202	 0.6027	 0.0	 28.0	 28.0	 28.0	 0.0	 0.0	 1	 -30.0	 30.0;
	29	 30	 0.2399	 0.4533	 0.0	 28.0	 28.0	 28.0	 0.0	 0.0	 1	 -30.0	 30.0;
	8	 28	 0.0636	 0.2	 0.0428	 140.0	 140.0	 140.0	 0.0	 0.0	 1	 -30.0	 30.0;
	6	 28	 0.0169	 0.0599	 0.013	 149.0	 149.0	 149.0	 0.0	 0.0	 1	 -30.0	 30.0;
];

% INFO    : === Translation Options ===
% INFO    : Load Model:                  from file ./pglib_opf_case30_ieee.m.api.sol
% INFO    : Gen Active Capacity Model:   stat
% INFO    : Gen Reactive Capacity Model: al50ag
% INFO    : Gen Active Cost Model:       stat
% INFO    :
% INFO    : === Load Replacement Notes ===
% INFO    : Bus 2	: Pd=21.7, Qd=12.7 -> Pd=36.08, Qd=12.70
% INFO    : Bus 3	: Pd=2.4, Qd=1.2 -> Pd=3.99, Qd=1.20
% INFO    : Bus 4	: Pd=7.6, Qd=1.6 -> Pd=12.64, Qd=1.60
% INFO    : Bus 5	: Pd=94.2, Qd=19.0 -> Pd=156.64, Qd=19.00
% INFO    : Bus 7	: Pd=22.8, Qd=10.9 -> Pd=37.91, Qd=10.90
% INFO    : Bus 8	: Pd=30.0, Qd=30.0 -> Pd=49.89, Qd=30.00
% INFO    : Bus 10	: Pd=5.8, Qd=2.0 -> Pd=9.64, Qd=2.00
% INFO    : Bus 12	: Pd=11.2, Qd=7.5 -> Pd=18.62, Qd=7.50
% INFO    : Bus 14	: Pd=6.2, Qd=1.6 -> Pd=10.31, Qd=1.60
% INFO    : Bus 15	: Pd=8.2, Qd=2.5 -> Pd=13.64, Qd=2.50
% INFO    : Bus 16	: Pd=3.5, Qd=1.8 -> Pd=5.82, Qd=1.80
% INFO    : Bus 17	: Pd=9.0, Qd=5.8 -> Pd=14.97, Qd=5.80
% INFO    : Bus 18	: Pd=3.2, Qd=0.9 -> Pd=5.32, Qd=0.90
% INFO    : Bus 19	: Pd=9.5, Qd=3.4 -> Pd=15.80, Qd=3.40
% INFO    : Bus 20	: Pd=2.2, Qd=0.7 -> Pd=3.66, Qd=0.70
% INFO    : Bus 21	: Pd=17.5, Qd=11.2 -> Pd=29.10, Qd=11.20
% INFO    : Bus 23	: Pd=3.2, Qd=1.6 -> Pd=5.32, Qd=1.60
% INFO    : Bus 24	: Pd=8.7, Qd=6.7 -> Pd=14.47, Qd=6.70
% INFO    : Bus 26	: Pd=3.5, Qd=2.3 -> Pd=5.82, Qd=2.30
% INFO    : Bus 29	: Pd=2.4, Qd=0.9 -> Pd=3.99, Qd=0.90
% INFO    : Bus 30	: Pd=10.6, Qd=1.9 -> Pd=17.63, Qd=1.90
% INFO    :
% INFO    : === Generator Setpoint Replacement Notes ===
% INFO    : Gen at bus 1	: Pg=135.5, Qg=5.0 -> Pg=254.0, Qg=49.0
% INFO    : Gen at bus 2	: Pg=46.0, Qg=3.0 -> Pg=261.0, Qg=-46.0
% INFO    : Gen at bus 5	: Pg=0.0, Qg=0.0 -> Pg=0.0, Qg=32.0
% INFO    : Gen at bus 8	: Pg=0.0, Qg=15.0 -> Pg=0.0, Qg=192.0
% INFO    : Gen at bus 11	: Pg=0.0, Qg=9.0 -> Pg=0.0, Qg=15.0
% INFO    : Gen at bus 13	: Pg=0.0, Qg=9.0 -> Pg=0.0, Qg=-1.0
% INFO    :
% INFO    : === Generator Reactive Capacity Atleast Setpoint Value Notes ===
% INFO    : Gen at bus 1	: Qg 49.0, Qmin 0.0, Qmax 10.0 -> Qmin -58.8, Qmax 58.8
% INFO    : Gen at bus 2	: Qg -46.0, Qmin -40.0, Qmax 46.0 -> Qmin -55.2, Qmax 46.0
% INFO    : Gen at bus 8	: Qg 192.0, Qmin -10.0, Qmax 40.0 -> Qmin -230.4, Qmax 230.4
% INFO    : Gen at bus 11	: Qg 15.0, Qmin -6.0, Qmax 24.0 -> Qmin -18.0, Qmax 24.0
% INFO    :
% INFO    : === Generator Classification Notes ===
% INFO    : SYNC   4   -     0.00
% INFO    : NG     2   -   100.00
% INFO    :
% INFO    : === Generator Active Capacity Stat Model Notes ===
% INFO    : Gen at bus 1 - NG	: Pg=254.0, Pmax=271.0 -> Pmax=351   samples: 5
% INFO    : Gen at bus 2 - NG	: Pg=261.0, Pmax=92.0 -> Pmax=342   samples: 12
% INFO    : Gen at bus 5 - SYNC	: Pg=0.0, Pmax=0.0 -> Pmax=0   samples: 0
% INFO    : Gen at bus 8 - SYNC	: Pg=0.0, Pmax=0.0 -> Pmax=0   samples: 0
% INFO    : Gen at bus 11 - SYNC	: Pg=0.0, Pmax=0.0 -> Pmax=0   samples: 0
% INFO    : Gen at bus 13 - SYNC	: Pg=0.0, Pmax=0.0 -> Pmax=0   samples: 0
% INFO    :
% INFO    : === Generator Active Capacity LB Model Notes ===
% INFO    :
% INFO    : === Generator Reactive Capacity Atleast Max 50 Percent Active Model Notes ===
% INFO    : Gen at bus 1 - NG	: Pmax 351.0, Qmin -58.8, Qmax 58.8 -> Qmin -176.0, Qmax 176.0
% INFO    : Gen at bus 2 - NG	: Pmax 342.0, Qmin -55.2, Qmax 46.0 -> Qmin -171.0, Qmax 171.0
% INFO    :
% INFO    : === Generator Setpoint Replacement Notes ===
% INFO    : Gen at bus 1	: Pg=254.0, Qg=49.0 -> Pg=175.5, Qg=0.0
% INFO    : Gen at bus 1	: Vg=1.0 -> Vg=1.0
% INFO    : Gen at bus 2	: Pg=261.0, Qg=-46.0 -> Pg=171.0, Qg=0.0
% INFO    : Gen at bus 2	: Vg=1.0 -> Vg=1.0
% INFO    : Gen at bus 5	: Pg=0.0, Qg=32.0 -> Pg=0.0, Qg=0.0
% INFO    : Gen at bus 5	: Vg=1.0 -> Vg=1.0
% INFO    : Gen at bus 8	: Pg=0.0, Qg=192.0 -> Pg=0.0, Qg=0.0
% INFO    : Gen at bus 8	: Vg=1.0 -> Vg=1.0
% INFO    : Gen at bus 11	: Pg=0.0, Qg=15.0 -> Pg=0.0, Qg=3.0
% INFO    : Gen at bus 11	: Vg=1.0 -> Vg=1.0
% INFO    : Gen at bus 13	: Pg=0.0, Qg=-1.0 -> Pg=0.0, Qg=9.0
% INFO    : Gen at bus 13	: Vg=1.0 -> Vg=1.0
% INFO    :
% INFO    : === Writing Matpower Case File Notes ===


function Y_l = load_regressor_UR10E(in1,in2,in3)
%LOAD_REGRESSOR_UR10E
%    Y_L = LOAD_REGRESSOR_UR10E(IN1,IN2,IN3)

%    This function was generated by the Symbolic Math Toolbox version 8.2.
%    20-Oct-2021 07:53:37

q2 = in1(2,:);
q3 = in1(3,:);
q4 = in1(4,:);
q5 = in1(5,:);
q6 = in1(6,:);
q2d1 = in3(1,:);
q2d2 = in3(2,:);
q2d3 = in3(3,:);
q2d4 = in3(4,:);
q2d5 = in3(5,:);
q2d6 = in3(6,:);
qd1 = in2(1,:);
qd2 = in2(2,:);
qd3 = in2(3,:);
qd4 = in2(4,:);
qd5 = in2(5,:);
qd6 = in2(6,:);
t2 = sin(1.57079632679);
t3 = cos(q2);
t4 = sin(q3);
t5 = cos(q3);
t6 = sin(q2);
t7 = sin(q5);
t8 = cos(q4);
t9 = sin(q4);
t10 = t2.^2;
t11 = qd1.*t2.*t3.*t5;
t24 = qd1.*t2.*t4.*t6;
t12 = t11-t24;
t13 = qd1.*t2.*t3.*t4;
t14 = qd1.*t2.*t5.*t6;
t15 = t13+t14;
t16 = t2.*t3.*t4;
t17 = t2.*t5.*t6;
t18 = t16+t17;
t19 = t2.*t3.*t5;
t21 = t2.*t4.*t6;
t20 = t19-t21;
t22 = t2.*t9.*t18;
t23 = t2.*t8.*t15;
t25 = t2.*t9.*t12;
t26 = t23+t25;
t27 = cos(q5);
t28 = qd2+qd3+qd4;
t29 = t27.*t28;
t41 = t7.*t26;
t30 = qd6+t29-t41;
t40 = t2.*t8.*t20;
t31 = t22-t40;
t32 = t7.*t10.*t30.*t31;
t33 = t7.^2;
t34 = t2.*t8.*t12;
t42 = t2.*t9.*t15;
t35 = t34-t42;
t36 = t2.*t8.*t18;
t37 = t2.*t9.*t20;
t38 = t36+t37;
t39 = t10.*t33.*t35.*t38;
t43 = sin(q6);
t44 = cos(q6);
t45 = t26.*t27;
t46 = t7.*t28;
t47 = t45+t46;
t48 = t38.*t43;
t49 = t26.*t43;
t62 = t27.*t35.*t44;
t50 = t49-t62;
t51 = t7.*t10.*t38.*t50;
t52 = qd5-t34+t42;
t53 = t44.*t47;
t63 = t43.*t52;
t54 = t53-t63;
t55 = t7.*t10.*t31.*t54;
t56 = t31.*t43;
t64 = t27.*t38.*t44;
t57 = t56-t64;
t58 = t7.*t10.*t35.*t57;
t59 = t27.*t31.*t44;
t60 = t48+t59;
t65 = t10.*t30.*t60;
t61 = t51+t55+t58-t65;
t66 = t10.*t27.*t57;
t67 = t10.*t33.*t38.*t44;
t68 = t31.*t44;
t69 = t27.*t38.*t43;
t70 = t68+t69;
t71 = t2.*t27.*t70;
t72 = t44.*t52;
t73 = t43.*t47;
t74 = t72+t73;
t75 = t29-t41;
t76 = t38.*t44;
t84 = t27.*t31.*t43;
t77 = t76-t84;
t78 = t2.*t30.*t77;
t79 = t26.*t44;
t80 = t27.*t35.*t43;
t81 = t79+t80;
t82 = t2.*t7.*t74.*(t22-t40);
t85 = t2.*t7.*t38.*t81;
t86 = t2.*t7.*t35.*t70;
t83 = t78+t82-t85-t86;
t87 = t10.*t50.*t57;
t88 = t27.*t44.*(t22-t40);
t89 = t48+t88;
t90 = t56-t64;
t91 = t2.*t7.*t43.*t57;
t93 = t2.*t7.*t44.*t70;
t92 = t91-t93;
t94 = t2.*t57.*t81;
t95 = t2.*t50.*t70;
t96 = t2.*t74.*t89;
t97 = t74.*t77;
t98 = t81.*(t68+t69);
t99 = t97+t98;
t100 = t68+t69;
t101 = t2.*t3.*t5.*(3.9e1./1.0e3);
t109 = t2.*t4.*t6.*(3.9e1./1.0e3);
t102 = t101-t109;
t103 = t2.*t3.*t4.*(3.9e1./1.0e3);
t104 = t2.*t5.*t6.*(3.9e1./1.0e3);
t105 = t103+t104;
t106 = t2.*t8.*t18.*(2.7e1./2.0e2);
t107 = t2.*t8.*t105;
t108 = t2.*t9.*t20.*(2.7e1./2.0e2);
t110 = t2.*t9.*t102;
t111 = t106+t107+t108+t110;
t112 = t2.*t9.*t18.*(2.7e1./2.0e2);
t113 = t2.*t9.*t105;
t120 = t2.*t8.*t20.*(2.7e1./2.0e2);
t121 = t2.*t8.*t102;
t114 = t112+t113-t120-t121;
t115 = t27.*t114;
t116 = t2.*t3.*(6.13e2./1.0e3);
t117 = t2.*t3.*t5.*(5.71e2./1.0e3);
t122 = t2.*t4.*t6.*(5.71e2./1.0e3);
t118 = t116+t117-t122;
t119 = t7.*t38.*(3.0./2.5e1);
t123 = t7.*t114;
t124 = t27.*t118;
t244 = t27.*t38.*(3.0./2.5e1);
t125 = t123+t124-t244;
t126 = qd2.*(6.13e2./1.0e3);
t127 = qd1.*t2.*t6.*(3.9e1./1.0e3);
t128 = t126+t127;
t129 = qd1.*t2.*t3.*(6.13e2./1.0e3);
t130 = qd1.*t2.*t3.*t5.*(5.71e2./1.0e3);
t155 = qd1.*t2.*t4.*t6.*(5.71e2./1.0e3);
t131 = t129+t130-t155;
t132 = t4.*t128;
t154 = qd1.*t2.*t3.*t5.*(3.9e1./1.0e3);
t133 = t132-t154;
t134 = t2.*t8.*t133;
t135 = t2.*t9.*t15.*(2.7e1./2.0e2);
t136 = qd2.*(5.71e2./1.0e3);
t137 = qd3.*(5.71e2./1.0e3);
t138 = t5.*t128;
t139 = qd1.*t2.*t3.*t4.*(3.9e1./1.0e3);
t140 = t136+t137+t138+t139;
t141 = t2.*t9.*t140;
t156 = t2.*t8.*t12.*(2.7e1./2.0e2);
t142 = t134+t135+t141-t156;
t143 = t26.*t27.*(3.0./2.5e1);
t144 = t7.*t28.*(3.0./2.5e1);
t149 = t7.*t118;
t145 = t115+t119-t149;
t146 = t43.*t145;
t147 = t31.*t43.*(1.17e2./1.0e3);
t148 = t43.*t111;
t150 = t44.*(t22-t40).*(1.17e2./1.0e3);
t151 = t27.*t38.*t43.*(1.17e2./1.0e3);
t152 = t44.*t145;
t153 = t148+t150+t151+t152;
t157 = t2.*t8.*t15.*(2.7e1./2.0e2);
t158 = t2.*t9.*t12.*(2.7e1./2.0e2);
t159 = t2.*t8.*t140;
t192 = t2.*t9.*t133;
t160 = t157+t158+t159-t192;
t161 = t27.*t142;
t162 = t7.*t26.*(3.0./2.5e1);
t249 = t27.*t131;
t250 = t7.*t142;
t163 = t143+t144-t249-t250;
t201 = t44.*t111;
t202 = t27.*t38.*t44.*(1.17e2./1.0e3);
t164 = t146+t147-t201-t202;
t194 = t7.*t131;
t195 = t27.*t28.*(3.0./2.5e1);
t165 = t161+t162-t194-t195;
t166 = t27.*t111;
t167 = t2.*t6.*(6.13e2./1.0e3);
t168 = t2.*t3.*t4.*(5.71e2./1.0e3);
t169 = t2.*t5.*t6.*(5.71e2./1.0e3);
t170 = t167+t168+t169;
t171 = t7.*t170;
t215 = t7.*t31.*(3.0./2.5e1);
t172 = t166+t171-t215;
t183 = qd1.*t2.*t4.*t6.*(3.9e1./1.0e3);
t173 = t154-t183;
t174 = qd1.*t2.*t5.*t6.*(3.9e1./1.0e3);
t175 = t139+t174;
t176 = qd1.*t2.*t6.*(6.13e2./1.0e3);
t177 = qd1.*t2.*t3.*t4.*(5.71e2./1.0e3);
t178 = qd1.*t2.*t5.*t6.*(5.71e2./1.0e3);
t179 = t176+t177+t178;
t180 = t7.*t179;
t181 = t7.*t35.*(3.0./2.5e1);
t182 = t2.*t8.*t175;
t184 = t2.*t9.*t173;
t185 = t157+t158+t182+t184;
t186 = t27.*t185;
t187 = t180+t181+t186;
t188 = t2.*t9.*t175;
t284 = t2.*t8.*t173;
t189 = t135-t156+t188-t284;
t190 = t44.*t52.*(1.17e2./1.0e3);
t191 = t43.*t47.*(1.17e2./1.0e3);
t193 = t43.*t160;
t196 = t44.*t165;
t197 = t190+t191+t193+t196;
t198 = t44.*t47.*(1.17e2./1.0e3);
t199 = t44.*t160;
t222 = t43.*t52.*(1.17e2./1.0e3);
t223 = t43.*t165;
t200 = t198+t199-t222-t223;
t203 = t26.*t43.*(1.17e2./1.0e3);
t204 = t138+t139;
t205 = t177+t178;
t206 = t7.*t205;
t207 = t2.*t8.*t204;
t208 = t157+t158-t192+t207;
t209 = t27.*t208;
t210 = t181+t206+t209;
t211 = t26.*t44.*(1.17e2./1.0e3);
t212 = t2.*t9.*t204;
t213 = t134+t135-t156+t212;
t214 = t27.*t35.*t43.*(1.17e2./1.0e3);
t216 = t38.*t44.*(1.17e2./1.0e3);
t217 = t168+t169;
t218 = t7.*t217;
t219 = t166-t215+t218;
t220 = t38.*t43.*(1.17e2./1.0e3);
t221 = t2.*t77.*t197;
t224 = t2.*t81.*t153;
t225 = t2.*t50.*t164;
t226 = t5.*(6.13e2./1.0e3);
t227 = t226+5.71e2./1.0e3;
t228 = t2.*t4.*t9.*(6.13e2./1.0e3);
t253 = t2.*t8.*t227;
t229 = t228-t253;
t230 = t27.*(3.0./2.5e1);
t231 = t2.*t9.*t227;
t232 = t2.*t4.*t8.*(6.13e2./1.0e3);
t233 = t231+t232;
t254 = t27.*t233;
t234 = t230-t254;
t235 = t44.*t114;
t236 = t166-t215;
t237 = t27.*t31.*t44.*(1.17e2./1.0e3);
t238 = t27.*t160;
t239 = t181+t238;
t240 = t7.*t44.*(1.17e2./1.0e3);
t245 = t2.*t9.*t27.*(5.71e2./1.0e3);
t241 = t230-t245;
t242 = t2.*t7.*t44.*t164;
t243 = t7.*t43.*(1.17e2./1.0e3);
t246 = t43.*t241;
t247 = t2.*t8.*t44.*(5.71e2./1.0e3);
t248 = t240+t246+t247;
t251 = t2.*t27.*t164;
t252 = t7.*(3.0./2.5e1);
t255 = t43.*t234;
t327 = t44.*t229;
t256 = t240+t255-t327;
t257 = t2.*t7.*t43.*t125;
t258 = t43.*t219;
t259 = t44.*(t112+t113-t120-t121);
t260 = t27.*t44.*(t22-t40).*(1.17e2./1.0e3);
t261 = t220+t258+t259+t260;
t262 = t43.*t210;
t263 = t44.*t213;
t278 = t27.*t35.*t44.*(1.17e2./1.0e3);
t264 = t203+t262+t263-t278;
t265 = t27.*t43.*(3.0./2.5e1);
t266 = t240+t265;
t267 = t43.*t125;
t343 = t7.*t38.*t44.*(1.17e2./1.0e3);
t268 = t267-t343;
t269 = t44.*t75.*(1.17e2./1.0e3);
t358 = t43.*t163;
t270 = t269-t358;
t271 = t27.*t35.*(3.0./2.5e1);
t272 = t7.*t111;
t273 = t27.*t31.*(3.0./2.5e1);
t274 = t43.*t236;
t275 = t220+t235+t237+t274;
t276 = t44.*t142;
t277 = t43.*t239;
t279 = t2.*t77.*t163;
t280 = t43.*t172;
t281 = t2.*t7.*t31.*t200;
t282 = t2.*t7.*t35.*t164;
t283 = t43.*t187;
t285 = t44.*t189;
t286 = t27.*t205;
t335 = t7.*t208;
t287 = t271+t286-t335;
t336 = t27.*t217;
t288 = t272+t273-t336;
t289 = t44.*t219;
t311 = t43.*t114;
t313 = t27.*t31.*t43.*(1.17e2./1.0e3);
t290 = t216+t289-t311-t313;
t291 = t44.*t210;
t337 = t43.*t213;
t292 = t211+t214+t291-t337;
t331 = t2.*t7.*t9.*(5.71e2./1.0e3);
t293 = t252-t331;
t294 = t2.*t8.*t43.*(5.71e2./1.0e3);
t332 = t44.*t241;
t295 = t243+t294-t332;
t296 = t44.*t125;
t297 = t7.*t38.*t43.*(1.17e2./1.0e3);
t298 = t296+t297;
t299 = t43.*t75.*(1.17e2./1.0e3);
t300 = t44.*t163;
t301 = t299+t300;
t302 = t10.*t27.*t153;
t328 = t7.*t233;
t303 = t252-t328;
t304 = t7.*t10.*t44.*t125;
t305 = t43.*t229;
t306 = t44.*t234;
t307 = t44.*(t115+t119-t149);
t308 = t148+t150+t151+t307;
t330 = t27.*t44.*(3.0./2.5e1);
t309 = t243-t330;
t310 = t272+t273;
t312 = t44.*t236;
t346 = t7.*t160;
t314 = t271-t346;
t315 = t10.*t89.*t163;
t316 = t44.*t239;
t345 = t43.*t142;
t317 = t211+t214+t316-t345;
t318 = t7.*t10.*t31.*t197;
t319 = t27.*t179;
t340 = t7.*t185;
t320 = t271+t319-t340;
t339 = t27.*t170;
t321 = t272+t273-t339;
t322 = t44.*t172;
t323 = t44.*t187;
t341 = t43.*t189;
t324 = t211+t214+t323-t341;
t325 = t146+t147-t201-t202;
t326 = t123+t124-t244;
t329 = -t243+t305+t306;
t333 = t44.*(t161+t162-t194-t195);
t334 = t190+t191+t193+t333;
t338 = t220+t235+t237+t280;
t342 = t216-t311-t313+t322;
t344 = t203+t276+t277-t278;
t347 = t216-t311+t312-t313;
t348 = t27.^2;
t349 = t10.*t27.*t50;
t350 = t10.*t33.*t35.*t44;
t351 = t349+t350;
t352 = t2.*t27.*t81;
t354 = t2.*t33.*t35.*t43;
t353 = t352-t354;
t355 = t44.^2;
t356 = t2.*t7.*t43.*t50;
t357 = t43.^2;
t359 = t44.*(t135-t156+t188-t284);
t360 = t203-t278+t283+t359;
t361 = t2.*t50.*t256;
t362 = t2.*t81.*t329;
t363 = t2.*t5.*t9.*(6.13e2./1.0e3);
t364 = t232+t363;
t385 = t2.*t5.*t8.*(6.13e2./1.0e3);
t365 = t228-t385;
t366 = t2.*t57.*t256;
t367 = t2.*t7.*t44.*t256;
t368 = t2.*t7.*t38.*t256;
t369 = t251+t257+t368-t2.*t70.*t303;
t370 = t2.*t27.*t256;
t371 = t2.*t7.*t43.*t303;
t372 = t44.*t233;
t412 = t27.*t43.*t229;
t373 = t372-t412;
t374 = t2.*t81.*t303;
t375 = t2.*t3.*t4.*(9.81e2./1.0e2);
t376 = t2.*t5.*t6.*(9.81e2./1.0e2);
t377 = t375+t376;
t378 = t2.*t3.*t5.*(9.81e2./1.0e2);
t380 = t2.*t4.*t6.*(9.81e2./1.0e2);
t379 = t378-t380;
t381 = t2.*t9.*t377;
t382 = t27.*t44.*(1.17e2./1.0e3);
t426 = t43.*t303;
t383 = t382-t426;
t384 = t44.*t364;
t405 = t27.*t43.*t365;
t386 = t384-t405;
t387 = t43.*t364;
t388 = t27.*t43.*(1.17e2./1.0e3);
t389 = t44.*t303;
t390 = t388+t389;
t391 = t10.*t50.*t303;
t392 = t43.*t233;
t393 = t27.*t44.*t229;
t394 = t392+t393;
t395 = t7.*t10.*t35.*t329;
t396 = t10.*t27.*t308;
t397 = t10.*t57.*t303;
t398 = t7.*t10.*t38.*t329;
t399 = t7.*t10.*t44.*t303;
t400 = t2.*t8.*t377;
t401 = t2.*t9.*t379;
t402 = t400+t401;
t421 = t2.*t8.*t379;
t403 = t381-t421;
t404 = -t243+t305+t306;
t406 = t27.*t44.*(t228-t385);
t407 = t387+t406;
t408 = t164.*t256;
t409 = t10.*t125.*t303;
t410 = t10.*t308.*t329;
t411 = t408+t409+t410;
t413 = t2.*t6.*(1.81e2./1.0e3);
t414 = t413-6.13e2./1.0e3;
t415 = t4.*t414;
t422 = t2.*t3.*t5.*(1.81e2./1.0e3);
t416 = t415-t422;
t417 = t5.*t414;
t418 = t2.*t3.*t4.*(1.81e2./1.0e3);
t419 = t417+t418-5.71e2./1.0e3;
t420 = t44.*t402;
t438 = t2.*t4.*t6.*(1.81e2./1.0e3);
t423 = t422-t438;
t424 = t2.*t5.*t6.*(1.81e2./1.0e3);
t425 = t418+t424;
t427 = t7.*(8.7e1./5.0e2);
t428 = t2.*t9.*t416;
t443 = t2.*t8.*t419;
t429 = t428-t443;
t580 = t27.*t429;
t430 = t427-t580;
t431 = t2.*t8.*t416;
t432 = t2.*t9.*t419;
t433 = t431+t432+3.0./2.5e1;
t434 = t43.*t402;
t435 = t27.*t44.*t403;
t436 = t434+t435;
t437 = t2.*t8.*t425;
t439 = t2.*t9.*t423;
t440 = t437+t439;
t441 = t2.*t8.*t423;
t444 = t2.*t9.*t425;
t442 = t441-t444;
t445 = t7.*t10.*t30;
t446 = t10.*t27.*t47;
t447 = t445+t446;
t448 = q2d6.*t10.*t27;
t449 = q2d2.*t10.*t348;
t450 = q2d3.*t10.*t348;
t451 = q2d4.*t10.*t348;
t452 = t7.*t10.*t30.*t35;
t607 = qd5.*t447;
t608 = qd2.*t7.*t10.*t27.*t35;
t609 = qd3.*t7.*t10.*t27.*t35;
t610 = qd4.*t7.*t10.*t27.*t35;
t611 = q2d1.*t7.*t10.*t27.*t38;
t453 = t448+t449+t450+t451+t452-t607-t608-t609-t610-t611;
t454 = qd2.*t351;
t455 = qd3.*t351;
t456 = qd4.*t351;
t457 = t7.*t10.*t54;
t458 = t7.*t10.*t44.*t47;
t612 = t10.*t27.*t30.*t44;
t613 = t10.*t27.*t44.*(t29-t41);
t459 = qd5.*(t457+t458-t612-t613);
t460 = q2d1.*(t66+t67);
t461 = t10.*t27.*t74;
t462 = t7.*t10.*t30.*t43;
t463 = t461+t462;
t464 = qd6.*t463;
t465 = q2d5.*t10.*t27.*t43;
t614 = t10.*t30.*t50;
t615 = t7.*t10.*t35.*t54;
t616 = q2d6.*t7.*t10.*t44;
t617 = q2d2.*t7.*t10.*t27.*t44.*2.0;
t618 = q2d3.*t7.*t10.*t27.*t44.*2.0;
t619 = q2d4.*t7.*t10.*t27.*t44.*2.0;
t466 = t454+t455+t456+t459+t460+t464+t465-t614-t615-t616-t617-t618-t619;
t467 = t2.*t7.*t74;
t468 = t2.*t7.*t43.*t47;
t620 = t2.*t27.*t43.*t75;
t621 = t2.*t27.*t30.*t43;
t469 = t467+t468-t620-t621;
t623 = t2.*t33.*t38.*t43;
t470 = q2d1.*(t71-t623);
t471 = t2.*t27.*t54;
t472 = t2.*t7.*t30.*t44;
t473 = qd6.*(t471+t472);
t474 = qd2.*t353;
t475 = qd3.*t353;
t476 = qd4.*t353;
t477 = t2.*t7.*t35.*t74;
t478 = q2d5.*t2.*t27.*t44;
t479 = q2d6.*t2.*t7.*t43;
t480 = q2d2.*t2.*t7.*t27.*t43.*2.0;
t481 = q2d3.*t2.*t7.*t27.*t43.*2.0;
t482 = q2d4.*t2.*t7.*t27.*t43.*2.0;
t622 = qd5.*t469;
t624 = t2.*t30.*t81;
t483 = t470+t473+t474+t475+t476+t477+t478+t479+t480+t481+t482-t622-t624;
t484 = t7.*t10.*t44.*t74;
t485 = t7.*t10.*t43.*t54;
t486 = t484+t485;
t487 = t7.*t10.*t75.*t355;
t488 = t10.*t27.*t44.*t54;
t489 = qd5.*(t487+t488);
t490 = t10.*t50.*(t53-t63);
t491 = q2d2.*t10.*t33.*t355;
t492 = q2d3.*t10.*t33.*t355;
t493 = q2d4.*t10.*t33.*t355;
t625 = qd6.*t486;
t626 = q2d5.*t7.*t10.*t43.*t44;
t627 = q2d1.*t7.*t10.*t44.*t57;
t628 = qd2.*t7.*t10.*t44.*t50;
t629 = qd3.*t7.*t10.*t44.*t50;
t630 = qd4.*t7.*t10.*t44.*t50;
t494 = t489+t490+t491+t492+t493-t625-t626-t627-t628-t629-t630;
t495 = q2d1.*t92;
t497 = t2.*t7.*t44.*t81;
t496 = qd2.*(t356-t497);
t498 = t2.*t7.*t44.*t54.*2.0;
t633 = t2.*t7.*t43.*t74.*2.0;
t499 = t498-t633;
t500 = t2.*t27.*t44.*t74;
t501 = t2.*t27.*t43.*t54;
t502 = t2.*t7.*t43.*t44.*t75.*2.0;
t503 = t500+t501+t502;
t504 = t2.*t7.*t355;
t634 = t2.*t7.*t357;
t505 = t504-t634;
t506 = t2.*t81.*(t53-t63);
t507 = t7.*t75.*t357;
t508 = t27.*t43.*t74;
t509 = qd5.*(t507+t508);
t510 = t7.*t44.*t74;
t511 = t7.*t43.*(t53-t63);
t512 = t510+t511;
t513 = qd6.*t512;
t514 = q2d2.*t33.*t357;
t515 = q2d3.*t33.*t357;
t516 = q2d4.*t33.*t357;
t517 = qd2.*t7.*t43.*t81;
t518 = qd3.*t7.*t43.*t81;
t519 = qd4.*t7.*t43.*t81;
t520 = q2d5.*t7.*t43.*t44;
t521 = q2d1.*t7.*t43.*(t68+t69);
t636 = t74.*t81;
t522 = t509+t513+t514+t515+t516+t517+t518+t519+t520+t521-t636;
t523 = t2.*t7.*t43.*(t243-t330);
t524 = t2.*t7.*t44.*t248;
t525 = t2.*t7.*t43.*t295;
t526 = t2.*t7.*t44.*t266;
t527 = t2.*t7.*t44.*t264;
t528 = t2.*t50.*t248;
t529 = t2.*t7.*t44.*t360;
t530 = t2.*t7.*t44.*t344;
t639 = t2.*t7.*t43.*t329;
t531 = t367+t524+t525-t639;
t532 = t2.*t57.*t248;
t533 = t2.*t27.*t44.*t200;
t534 = t2.*t27.*t43.*t334;
t535 = t2.*t7.*t44.*t270;
t536 = t2.*t7.*t43.*t301;
t537 = t2.*t50.*t200;
t538 = t2.*t27.*t266;
t539 = t2.*t27.*t248;
t540 = t2.*t33.*t43.*(3.0./2.5e1);
t541 = t2.*t7.*t43.*t293;
t542 = t2.*t7.*t38.*t248;
t543 = t251+t257+t542-t2.*t70.*t293;
t544 = t2.*t27.*t43.*(1.17e2./1.0e3);
t545 = t2.*t9.*t44.*(5.71e2./1.0e3);
t546 = t2.*t8.*t27.*t43.*(5.71e2./1.0e3);
t547 = t545+t546;
t548 = t2.*t7.*t43.*t314;
t593 = t43.*t293;
t549 = t382-t593;
t550 = t2.*t27.*t270;
t551 = t2.*t27.*t43.*t163;
t552 = t2.*t7.*t43.*t287;
t553 = t2.*t7.*t35.*t248;
t554 = t2.*t7.*t44.*t163;
t555 = t370+t371+t539+t541;
t556 = t2.*t81.*t293;
t557 = t2.*t7.*t43.*t320;
t558 = t2.*t7.*t35.*t200;
t559 = t2.*t9.*t43.*(5.71e2./1.0e3);
t592 = t2.*t8.*t27.*t44.*(5.71e2./1.0e3);
t560 = t559-t592;
t561 = t10.*t27.*t317;
t562 = t44.*t293;
t563 = t388+t562;
t564 = t7.*t10.*t334;
t565 = t10.*t27.*t324;
t566 = t10.*t50.*t293;
t567 = t10.*t33.*t44.*(3.0./2.5e1);
t568 = t7.*t10.*t44.*t293;
t569 = t10.*t57.*t293;
t570 = t10.*t27.*t292;
t571 = t10.*t27.*t200;
t572 = t7.*t10.*t43.*t163;
t573 = t10.*t27.*t329;
t574 = t10.*t27.*t44.*(1.17e2./1.0e3);
t575 = t10.*t50.*(t143+t144-t249-t250);
t576 = t417+t418;
t577 = t44.*t403;
t578 = t27.*t43.*t402;
t579 = t577+t578;
t581 = t43.*t430;
t582 = t44.*t433;
t583 = t581+t582;
t660 = t27.*t43.*t403;
t584 = t420-t660;
t585 = t583.*t584;
t586 = t248.*t256;
t587 = t10.*t293.*t303;
t588 = t586+t587-t10.*t295.*t329;
t589 = t164.*t248;
t590 = t10.*t125.*t293;
t591 = t589+t590-t10.*t295.*t308;
t594 = t428-t2.*t8.*t576;
t595 = t2.*t9.*t576;
t596 = t431+t595;
t597 = t43.*t403;
t674 = t27.*t44.*t402;
t598 = t597-t674;
t599 = t44.*t430;
t675 = t43.*t433;
t600 = t599-t675;
t601 = t27.*t44.*(t381-t421);
t602 = t434+t601;
t603 = t27.*(8.7e1./5.0e2);
t604 = t7.*(t428-t443);
t605 = t603+t604+1.17e2./1.0e3;
t606 = t7.*t10.*t605.*(t381-t421);
t631 = qd3.*(t356-t497);
t632 = qd4.*(t356-t497);
t635 = t495+t496+t506+t631+t632-q2d5.*t505-qd6.*t499-qd5.*t503-t2.*t50.*t74-q2d2.*t2.*t33.*t43.*t44.*2.0-q2d3.*t2.*t33.*t43.*t44.*2.0-q2d4.*t2.*t33.*t43.*t44.*2.0;
t637 = t523+t524+t525+t526;
t638 = t2.*t50.*t266;
t640 = t2.*t57.*t266;
t641 = t2.*t7.*(t381-t421);
t642 = t538+t539+t540+t541;
t643 = t2.*t27.*t344;
t644 = t370+t371+t538+t540;
t661 = t7.*t43.*(3.0./2.5e1);
t645 = t382-t661;
t646 = t2.*t27.*t360;
t647 = t2.*t7.*t81.*(3.0./2.5e1);
t648 = t2.*t7.*t38.*t266;
t649 = t251+t257+t648-t2.*t7.*t70.*(3.0./2.5e1);
t650 = t2.*t602;
t651 = t7.*t44.*(3.0./2.5e1);
t652 = t388+t651;
t653 = t10.*t27.*t44.*t163;
t657 = t10.*t27.*t309;
t654 = t567+t568-t657-t10.*t27.*t295;
t655 = t7.*t10.*t50.*(3.0./2.5e1);
t656 = t10.*t27.*(-t243+t305+t306);
t658 = t7.*t10.*t57.*(3.0./2.5e1);
t659 = t304+t396+t658-t7.*t10.*t38.*t309;
t662 = t243-t330;
t663 = t256.*t266;
t664 = t7.*t10.*t303.*(3.0./2.5e1);
t665 = t663+t664-t10.*t309.*t329;
t666 = t164.*t266;
t667 = t7.*t10.*t125.*(3.0./2.5e1);
t668 = t666+t667-t10.*t308.*t309;
t669 = t248.*t266;
t670 = t10.*t295.*(t243-t330);
t671 = t7.*t10.*t293.*(3.0./2.5e1);
t672 = t669+t670+t671;
t673 = t431+t432;
t676 = t2.*t44.*t57;
t677 = t2.*t43.*t70;
t678 = t2.*t44.*t50;
t679 = t2.*t43.*t81;
t680 = t678+t679;
t681 = t2.*t43.*t256;
t682 = t2.*t44.*t329;
t683 = t681+t682;
t684 = t2.*t43.*t248;
t685 = t684-t2.*t44.*t295;
t686 = t2.*t43.*t266;
t687 = t686-t2.*t44.*t309;
t688 = t2.*t43.*t164;
t689 = t2.*t44.*t70.*(1.17e2./1.0e3);
t690 = t2.*t43.*t57.*(1.17e2./1.0e3);
t691 = t2.*t44.*t81.*(1.17e2./1.0e3);
t692 = t2.*t43.*t50.*(1.17e2./1.0e3);
t693 = t2.*t44.*t125;
t694 = t693-t2.*t7.*t38.*t43.*(1.17e2./1.0e3);
t695 = t544-t2.*t44.*t293;
t696 = t2.*t7.*t35.*t43.*(1.17e2./1.0e3);
t697 = t544-t2.*t44.*t303;
t698 = t544-t2.*t7.*t44.*(3.0./2.5e1);
t699 = t10.*t43.*t303;
t700 = t574+t699;
t701 = t7.*t10.*t43.*(3.0./2.5e1);
t702 = t574+t701;
t703 = t10.*t43.*t125;
t704 = t7.*t10.*t38.*t44.*(1.17e2./1.0e3);
t705 = t10.*t43.*t293;
t706 = t574+t705;
t707 = t43.*t256.*(1.17e2./1.0e3);
t708 = t10.*t44.*t329.*(1.17e2./1.0e3);
t709 = t707+t708;
t710 = t43.*t248.*(1.17e2./1.0e3);
t711 = t710-t10.*t44.*t295.*(1.17e2./1.0e3);
t712 = t43.*t266.*(1.17e2./1.0e3);
t713 = t712-t10.*t44.*t309.*(1.17e2./1.0e3);
t714 = t43.*t164.*(1.17e2./1.0e3);
t715 = t10.*t44.*t308.*(1.17e2./1.0e3);
t716 = t603+t604;
t717 = t53-t63;
Y_l = reshape([qd2.*(t32+t39)+qd3.*(t32+t39)+qd4.*(t32+t39)+qd5.*(t7.*t10.*t38.*t47-t10.*t27.*t30.*t38)-q2d6.*t7.*t10.*t38+q2d1.*t10.*t33.*t38.^2-q2d2.*t7.*t10.*t27.*t38-q2d3.*t7.*t10.*t27.*t38-q2d4.*t7.*t10.*t27.*t38,t453,t453,t453,t10.*t30.*t47,q2d6.*t10-(qd5.*t10.*(t7.*t28.*2.0+t26.*t27.*2.0))./2.0+q2d2.*t10.*t27+q2d3.*t10.*t27+q2d4.*t10.*t27-q2d1.*t7.*t10.*t38-qd2.*t7.*t10.*t35-qd3.*t7.*t10.*t35-qd4.*t7.*t10.*t35,q2d2.*(t66+t67)+q2d3.*(t66+t67)+q2d4.*(t66+t67)+qd6.*(t10.*t30.*t70-t7.*t10.*t38.*t74)-qd2.*t61-qd3.*t61-qd4.*t61+qd5.*(-t10.*t47.*t57+t10.*t27.*t38.*(t53-t63)+t7.*t10.*t30.*t38.*t44+t7.*t10.*t38.*t44.*t75)+q2d6.*t10.*(t56-t64)-q2d5.*t7.*t10.*t38.*t43-q2d1.*t7.*t10.*t38.*t57.*2.0,t466,t466,t466,q2d6.*t10.*t43-t10.*t47.*t54+q2d2.*t10.*t27.*t43+q2d3.*t10.*t27.*t43+q2d4.*t10.*t27.*t43+qd6.*t10.*t30.*t44-qd5.*t10.*t43.*t47+t10.*t30.*t44.*(t29-t41)-q2d1.*t7.*t10.*t38.*t43-qd2.*t7.*t10.*t35.*t43-qd3.*t7.*t10.*t35.*t43-qd4.*t7.*t10.*t35.*t43,q2d5.*t10.*t43+qd2.*t10.*t50+qd3.*t10.*t50+qd4.*t10.*t50+qd6.*t10.*t74-t10.*t30.*t74+q2d1.*t10.*(t56-t64)-q2d2.*t7.*t10.*t44-q2d3.*t7.*t10.*t44-q2d4.*t7.*t10.*t44-qd5.*t10.*t44.*t75,-qd6.*(t2.*t30.*t57+t2.*t7.*t38.*t54)+qd2.*t83+qd3.*t83+qd4.*t83-qd5.*(t2.*t47.*t70+t2.*t27.*t38.*t74+t2.*t7.*t30.*t38.*t43+t2.*t7.*t38.*t43.*t75)+q2d2.*(t71-t2.*t33.*t38.*t43)+q2d3.*(t71-t2.*t33.*t38.*t43)+q2d4.*(t71-t2.*t33.*t38.*t43)+q2d6.*t2.*(t68+t69)-q2d5.*t2.*t7.*t38.*t44-q2d1.*t2.*t7.*t38.*t70.*2.0,t483,t483,t483,q2d6.*t2.*t44+t2.*t47.*t74+q2d2.*t2.*t27.*t44+q2d3.*t2.*t27.*t44+q2d4.*t2.*t27.*t44-qd6.*t2.*t30.*t43-qd5.*t2.*t44.*t47-t2.*t30.*t43.*t75-q2d1.*t2.*t7.*t38.*t44-qd2.*t2.*t7.*t35.*t44-qd3.*t2.*t7.*t35.*t44-qd4.*t2.*t7.*t35.*t44,q2d1.*t2.*(t68+t69)+q2d5.*t2.*t44+qd2.*t2.*t81+qd3.*t2.*t81+qd4.*t2.*t81-t2.*t30.*t54+qd6.*t2.*(t53-t63)+q2d2.*t2.*t7.*t43+q2d3.*t2.*t7.*t43+q2d4.*t2.*t7.*t43+qd5.*t2.*t43.*(t29-t41),-qd5.*(t10.*t44.*t57.*t75+t7.*t10.*t38.*t44.*t54)+qd2.*(t87-t10.*t54.*t60)+qd3.*(t87-t10.*t89.*(t53-t63))+qd4.*(t87-t10.*t89.*(t53-t63))+qd6.*(t10.*t57.*t74-t10.*(t68+t69).*(t53-t63))+q2d1.*t10.*t90.^2+q2d5.*t10.*t43.*(t56-t64)-q2d2.*t7.*t10.*t44.*t57-q2d3.*t7.*t10.*t44.*t57-q2d4.*t7.*t10.*t44.*t57,t494,t494,t494,-qd6.*(t10.*t44.*t54-t10.*t43.*t74)+q2d5.*t10.*t357+qd2.*t10.*t43.*t50+qd3.*t10.*t43.*t50+qd4.*t10.*t43.*t50-t10.*t44.*t54.*t75+q2d1.*t10.*t43.*(t56-t64)-q2d2.*t7.*t10.*t43.*t44-q2d3.*t7.*t10.*t43.*t44-q2d4.*t7.*t10.*t43.*t44-qd5.*t10.*t43.*t44.*t75,t10.*t74.*(t53-t63),q2d5.*(t676+t677)+q2d2.*t92+q2d3.*t92+q2d4.*t92+qd2.*(t94+t95+t96-t2.*t77.*(t53-t63))+qd3.*(t94+t95+t96-t2.*t77.*(t53-t63))+qd4.*(t94+t95+t96-t2.*t77.*(t53-t63))+qd6.*(t2.*t54.*t57.*2.0+t2.*t70.*t74.*2.0)+qd5.*(t2.*t43.*t57.*t75-t2.*t44.*(t68+t69).*(t29-t41)+t2.*t7.*t38.*t43.*t54+t2.*t7.*t38.*t44.*t74)+q2d1.*t2.*(t68+t69).*(t56-t64).*2.0,t495+t496+t506-q2d5.*t505-qd6.*t499-qd5.*t503+qd3.*(t356-t2.*t7.*t44.*t81)+qd4.*(t356-t2.*t7.*t44.*t81)-t2.*t50.*t74-q2d2.*t2.*t33.*t43.*t44.*2.0-q2d3.*t2.*t33.*t43.*t44.*2.0-q2d4.*t2.*t33.*t43.*t44.*2.0,t635,t635,q2d1.*(t676+t677)-q2d2.*t505-q2d3.*t505-q2d4.*t505+qd2.*t680+qd3.*t680+qd4.*t680-qd5.*(t2.*t75.*t355-t2.*t75.*t357)+qd6.*(t2.*t44.*t74.*2.0+t2.*t43.*(t53-t63).*2.0)+q2d5.*t2.*t43.*t44.*2.0+t2.*t44.*t74.*(t29-t41)+t2.*t43.*(t29-t41).*(t53-t63),-t2.*t74.^2+t2.*t717.^2,qd5.*(t43.*(t68+t69).*(t29-t41)-t7.*t38.*t43.*t74)+qd2.*t99+qd3.*t99+qd4.*t99+q2d1.*t100.^2+qd6.*(t54.*t70-t57.*t74)+q2d5.*t44.*(t68+t69)+q2d2.*t7.*t43.*(t68+t69)+q2d3.*t7.*t43.*(t68+t69)+q2d4.*t7.*t43.*(t68+t69),t522,t522,t522,q2d5.*t355+qd6.*(t44.*t54-t43.*t74)+q2d1.*t44.*(t68+t69)+qd2.*t44.*t81+qd3.*t44.*t81+qd4.*t44.*t81-t43.*t74.*t75+q2d2.*t7.*t43.*t44+q2d3.*t7.*t43.*t44+q2d4.*t7.*t43.*t44+qd5.*t43.*t44.*(t29-t41),-t54.*t74,-q2d3.*(t242+t532-t2.*t70.*t295-t2.*t7.*t43.*t153)-q2d4.*(t242+t640-t2.*t70.*t309-t2.*t7.*t43.*t153)-q2d2.*(t242+t366+t2.*t70.*(t305+t306-t7.*t43.*(1.17e2./1.0e3))-t2.*t7.*t43.*t153)+qd4.*(t221+t224+t225+t2.*t74.*(t216+t312-t43.*t114-t27.*t31.*t43.*(1.17e2./1.0e3))-t2.*t89.*t200+t2.*t70.*t317+t2.*t57.*(t203+t276+t277-t27.*t35.*t44.*(1.17e2./1.0e3))-t2.*t275.*(t53-t63))+qd2.*(t221+t224+t225+t2.*t74.*(t216+t322-t43.*t114-t27.*t31.*t43.*(1.17e2./1.0e3))-t2.*t89.*t200+t2.*t70.*t324+t2.*t57.*(t203+t283+t285-t27.*t35.*t44.*(1.17e2./1.0e3))-t2.*t338.*(t53-t63))+q2d5.*(t688+t689+t690+t2.*t44.*t153)+qd3.*(t221+t224+t225-t2.*t89.*t200+t2.*t57.*t264+t2.*t70.*t292+t2.*t74.*t290-t2.*t261.*(t53-t63))+q2d1.*(t2.*t153.*(t68+t69).*2.0+t2.*t57.*t164.*2.0)-qd5.*(-t2.*t54.*t268+t2.*t57.*t270-t2.*t70.*t301+t2.*t74.*t298-t2.*t43.*t75.*t153+t2.*t44.*t75.*t164+t2.*t7.*t38.*t43.*t197+t2.*t7.*t38.*t44.*t200),t537-q2d1.*(t242+t366+t2.*t70.*t329-t2.*t7.*t43.*t308)-qd4.*(t361+t362+t530+t2.*t54.*t373+t2.*t74.*t394-t2.*t7.*t43.*t317)+q2d3.*t531-q2d5.*t683-qd3.*(t361+t362+t527+t2.*t54.*t386+t2.*t74.*(t387+t27.*t44.*t365)-t2.*t7.*t43.*t292)+q2d2.*(t2.*t7.*t44.*t256.*2.0-t2.*t7.*t43.*t329.*2.0)+qd5.*(t533+t534+t535+t536+t2.*t54.*t383+t2.*t74.*t390-t2.*t43.*(t29-t41).*(-t243+t305+t306)+t2.*t44.*t75.*t256)+q2d4.*(t367+t523+t526-t2.*t7.*t43.*t329)-qd2.*(t361+t362+t529-t2.*t7.*t43.*t324)-t2.*t74.*t324+t2.*t7.*t403-t2.*t81.*t334+t2.*t360.*(t53-t63),t537+t641-q2d1.*(t242+t532-t2.*t70.*t295-t2.*t7.*t43.*t308)-qd3.*(t527+t528-t2.*t81.*t295-t2.*t7.*t43.*t292)-qd2.*(t528+t529-t2.*t81.*t295-t2.*t7.*t43.*t324)+q2d2.*t531+q2d4.*t637-q2d5.*t685-qd4.*(t528+t530-t2.*t81.*t295+t2.*t54.*t547+t2.*t74.*t560-t2.*t7.*t43.*t317)+q2d3.*(t2.*t7.*t44.*t248.*2.0+t2.*t7.*t43.*t295.*2.0)+qd5.*(t533+t534+t535+t536+t2.*t54.*t549+t2.*t74.*t563+t2.*t44.*t75.*t248+t2.*t43.*t75.*t295)-t2.*t74.*t292-t2.*t81.*t334+t2.*t264.*(t53-t63),t537+t641-q2d1.*(t242+t640-t2.*t70.*t309-t2.*t7.*t43.*t308)-qd3.*(t527+t638-t2.*t81.*t309-t2.*t7.*t43.*t292)-qd4.*(t530+t638-t2.*t81.*t309-t2.*t7.*t43.*t317)-qd2.*(t529+t638-t2.*t81.*t309-t2.*t7.*t43.*t324)+qd5.*(t533+t534+t2.*t74.*t652+t2.*t645.*(t53-t63)+t2.*t7.*t43.*(t299+t300)+t2.*t7.*t44.*(t269-t43.*(t143+t144-t249-t250))+t2.*t44.*t266.*(t29-t41)+t2.*t43.*(t29-t41).*(t243-t330))+q2d3.*t637-q2d5.*t687+q2d2.*(t367+t523+t526-t639)+q2d4.*(t2.*t7.*t44.*t266.*2.0+t2.*t7.*t43.*t309.*2.0)-t2.*t74.*t317-t2.*t81.*t334+t2.*t344.*(t53-t63),-q2d2.*t683-q2d3.*t685-q2d4.*t687+q2d1.*(t688+t689+t690+t2.*t44.*t308)+qd5.*(t2.*t44.*(t299+t300)-t2.*t43.*t270)+qd3.*(t691+t692+t2.*t43.*t264+t2.*t44.*t292)+qd4.*(t691+t692+t2.*t44.*t317+t2.*t43.*t344)+qd2.*(t691+t692+t2.*t44.*t324+t2.*t43.*t360)+q2d5.*(t2.*t355.*(1.17e2./5.0e2)+t2.*t357.*(1.17e2./5.0e2))-t2.*t54.*t270-t2.*t74.*t301-t2.*t27.*t402-t2.*t44.*t75.*t200-t2.*t43.*t75.*t334,0.0,-q2d1.*(t2.*t70.*t125.*2.0-t2.*t7.*t38.*t164.*2.0)-q2d2.*t369-q2d3.*t543-q2d4.*t649-q2d5.*t694-qd5.*(-t2.*t47.*t164+t2.*t74.*t145+t2.*t70.*t165-t2.*t30.*t268+t2.*t43.*t75.*t125+t2.*t27.*t38.*t200+t2.*t7.*t38.*t270+t2.*t7.*t38.*t43.*t163)+qd3.*(t279+t281+t282-t2.*t81.*(t123+t124-t244)-t2.*t30.*t261+t2.*t70.*t287-t2.*t74.*t288+t2.*t7.*t38.*t264)+qd4.*(t2.*t77.*(t143+t144-t249-t250)+t2.*t314.*(t68+t69)-t2.*t81.*t125-t2.*t30.*t275-t2.*t74.*t310+t2.*t7.*t38.*t344+t2.*t7.*t200.*(t22-t40)+t2.*t7.*t35.*(t146+t147-t201-t202))+qd2.*(t279+t281+t282-t2.*t81.*(t123+t124-t244)-t2.*t30.*t338+t2.*t70.*t320-t2.*t74.*t321+t2.*t7.*t38.*(t203-t278+t283+t285))-qd6.*(t2.*t54.*t125+t2.*t30.*t153+t2.*t57.*t163-t2.*t7.*t38.*t197)-q2d6.*t2.*t164,t558+qd2.*(t374+t557-t2.*t27.*t360-t2.*t7.*t35.*t256)+q2d2.*(t2.*t27.*t256.*2.0+t2.*t7.*t43.*t303.*2.0)-q2d1.*t369+q2d3.*t555+q2d4.*t644-q2d5.*t697+t2.*t436+qd6.*(t554+t2.*t54.*t303+t2.*t30.*t329-t2.*t27.*t334)+qd5.*(t550+t551-t2.*t7.*t200-t2.*t47.*t256+t2.*t74.*t234+t2.*t30.*t383+t2.*t43.*t75.*t303-t2.*t7.*t43.*(t161+t162-t194-t195))+qd4.*(t374+t548-t2.*t27.*t344-t2.*t30.*t373-t2.*t7.*t35.*t256+t2.*t7.*t74.*t229)+qd3.*(t374+t552-t2.*t27.*t264-t2.*t30.*t386-t2.*t7.*t35.*t256+t2.*t7.*t74.*t365)+q2d6.*t2.*t256-t2.*t81.*t163+t2.*t30.*t360-t2.*t74.*t320,t558+t650+q2d3.*(t2.*t27.*t248.*2.0+t2.*t7.*t43.*t293.*2.0)-q2d1.*t543+q2d2.*t555+q2d4.*t642-q2d5.*t695-qd4.*(-t548+t553+t643-t2.*t81.*t293+t2.*t30.*t547+t7.*t8.*t10.*t74.*(5.71e2./1.0e3))+qd3.*(t552-t553+t556-t2.*t27.*t264)-qd2.*(t553-t556-t557+t646)+qd6.*(t554-t2.*t30.*t295+t2.*t54.*t293-t2.*t27.*t334)+qd5.*(t550+t551-t2.*t7.*t200-t2.*t47.*t248+t2.*t74.*t241+t2.*t30.*t549+t2.*t43.*t75.*t293-t2.*t7.*t43.*(t161+t162-t194-t195))+q2d6.*t2.*t248-t2.*t81.*t163+t2.*t30.*t264-t2.*t74.*t287,t558+t650+qd3.*(t552+t647-t2.*t27.*t264-t2.*t7.*t35.*t266)+qd6.*(t554+t2.*t7.*t54.*(3.0./2.5e1)-t2.*t27.*t334-t2.*t30.*(t243-t330))+qd4.*(t548-t643+t647-t2.*t7.*t35.*t266)+qd2.*(t557-t646+t647-t2.*t7.*t35.*t266)+q2d3.*t642+q2d2.*t644-q2d1.*t649-q2d5.*t698+q2d4.*(t2.*t33.*t43.*(6.0./2.5e1)+t2.*t27.*t266.*2.0)+qd5.*(t550+t551+t2.*t27.*t74.*(3.0./2.5e1)-t2.*t7.*t200-t2.*t47.*t266+t2.*t30.*t645+t2.*t7.*t43.*t75.*(3.0./2.5e1)-t2.*t7.*t43.*(t161+t162-t194-t195))+q2d6.*t2.*t266-t2.*t81.*t163+t2.*t30.*t344-t2.*t74.*t314,-q2d1.*t694-q2d3.*t695-q2d2.*t697-q2d4.*t698+qd3.*(t696+t2.*t44.*t287)+qd4.*(t696+t2.*t44.*t314)+qd2.*(t696+t2.*t44.*t320)-qd6.*(t2.*t30.*t44.*(1.17e2./1.0e3)+t2.*t43.*t163)+qd5.*(t2.*t43.*t47.*(1.17e2./1.0e3)-t2.*t44.*t165)+t2.*t74.*(t161+t162-t194-t195)-q2d6.*t2.*t43.*(1.17e2./1.0e3)+t2.*t47.*t200-t2.*t30.*t270-t2.*t43.*t75.*t163+t2.*t7.*t44.*t402,t2.*(t577+t578)-q2d5.*t2.*t43.*(1.17e2./1.0e3)-q2d1.*t2.*t164+q2d3.*t2.*t248+q2d2.*t2.*t256+q2d4.*t2.*t266-qd3.*t2.*t264-qd6.*t2.*t334-qd4.*t2.*t344-qd2.*t2.*t360-t2.*t54.*t163+t2.*t30.*t334+qd5.*t2.*(t269-t43.*(t143+t144-t249-t250)),q2d5.*(t703+t704)-q2d4.*t659-qd3.*(t315+t318-t10.*t50.*t125+t10.*t30.*t290+t10.*t54.*t288+t10.*t57.*t287-t7.*t10.*t35.*t153-t7.*t10.*t38.*t292)-qd4.*(t315+t318-t10.*t50.*t125+t10.*t54.*t310+t10.*t57.*t314+t10.*t30.*t347-t7.*t10.*t35.*t308-t7.*t10.*t38.*t317)-qd2.*(t315+t318-t10.*t50.*t125+t10.*t30.*t342+t10.*t54.*t321+t10.*t57.*t320-t7.*t10.*t35.*t308-t7.*t10.*t38.*t324)+qd5.*(-t10.*(t53-t63).*(t115+t119-t149)+t10.*t47.*t153+t10.*t57.*t165+t10.*t30.*t298-t10.*t44.*(t29-t41).*(t123+t124-t244)+t10.*t27.*t38.*t197+t7.*t10.*t38.*t301-t7.*t10.*t38.*t44.*(t143+t144-t249-t250))-q2d3.*(t302+t304+t569-t7.*t10.*t38.*t295)+qd6.*(-t10.*(t68+t69).*(t143+t144-t249-t250)+t10.*t30.*t164+t10.*t74.*t125+t7.*t10.*t38.*t200)+q2d1.*(t10.*(t56-t64).*(t123+t124-t244).*2.0+t7.*t10.*t38.*t308.*2.0)-q2d2.*(t302+t304+t397+t398)-q2d6.*t10.*t308,t420+t575-qd3.*(t570+t10.*t50.*t303-t10.*t30.*t407-t7.*t10.*t44.*t287+t7.*t10.*t35.*t329-t7.*t10.*(t53-t63).*(t228-t385))+q2d2.*(t10.*t27.*t329.*2.0+t7.*t10.*t44.*t303.*2.0)-q2d5.*t700+q2d3.*(t399+t568+t573-t10.*t27.*t295)+q2d4.*(t399+t567+t656-t10.*t27.*t309)+qd5.*(t564+t653+t10.*t54.*t234-t10.*t27.*t301-t10.*t47.*t329-t10.*t30.*t390-t7.*t10.*t44.*t165+t10.*t44.*t75.*t303)-qd6.*(t571+t572+t10.*t30.*t256+t10.*t74.*t303)-qd4.*(t391+t395+t561-t10.*t30.*t394-t7.*t10.*t54.*t229-t7.*t10.*t44.*t314)-qd2.*(t391+t395+t565-t7.*t10.*t44.*t320)-q2d1.*(t304+t396+t397+t398)+q2d6.*t10.*(-t243+t305+t306)+t10.*t30.*t324-t10.*t54.*t320-t27.*t43.*t403-t7.*t10.*t35.*t334,t420+t575+qd5.*(t564-t10.*t27.*t301+t10.*t47.*t295-t10.*t30.*t563+t10.*t241.*(t53-t63)-t7.*t10.*t44.*t165+t10.*t44.*t293.*(t29-t41)+t10.*t27.*t44.*(t143+t144-t249-t250))-q2d3.*(t10.*t27.*t295.*2.0-t7.*t10.*t44.*t293.*2.0)+q2d4.*t654-q2d5.*t706+q2d2.*(t399+t568+t573-t10.*t27.*t295)-qd3.*(t566+t570-t7.*t10.*t35.*t295-t7.*t10.*t44.*t287)-qd2.*(t565+t566-t7.*t10.*t35.*t295-t7.*t10.*t44.*t320)-qd6.*(t571+t572+t10.*t30.*t248+t10.*t74.*t293)-q2d1.*(t304+t396+t569-t7.*t10.*t38.*t295)-qd4.*(t561+t566-t10.*t30.*t560-t7.*t10.*t35.*t295-t7.*t10.*t44.*t314+t2.*t7.*t8.*t10.*t54.*(5.71e2./1.0e3))-q2d6.*t10.*t295+t10.*t30.*t292-t10.*t54.*t287-t27.*t43.*t403-t7.*t10.*t35.*t334,t420+t575-t660+q2d3.*t654-q2d1.*t659-q2d5.*t702+q2d2.*(t399+t567+t656-t657)-qd3.*(t570+t655-t7.*t10.*t44.*t287-t7.*t10.*t35.*t309)-qd4.*(t561+t655-t7.*t10.*t35.*t309-t7.*t10.*t44.*t314)-qd2.*(t565+t655-t7.*t10.*t35.*t309-t7.*t10.*t44.*t320)-qd6.*(t571+t572+t7.*t10.*t74.*(3.0./2.5e1)+t10.*t30.*t266)+q2d4.*(t10.*t33.*t44.*(6.0./2.5e1)-t10.*t27.*t309.*2.0)+qd5.*(t564+t653-t10.*t27.*(t299+t300)+t10.*t27.*t54.*(3.0./2.5e1)+t10.*t47.*t309-t10.*t30.*t652+t7.*t10.*t44.*t75.*(3.0./2.5e1)-t7.*t10.*t44.*(t161+t162-t194-t195))-q2d6.*t10.*t309+t10.*t30.*t317-t10.*t54.*t314-t7.*t10.*t35.*t334,q2d1.*(t703+t704)-qd3.*(t10.*t43.*t287-t7.*t10.*t35.*t44.*(1.17e2./1.0e3))-qd4.*(t10.*t43.*t314-t7.*t10.*t35.*t44.*(1.17e2./1.0e3))-qd2.*(t10.*t43.*t320-t7.*t10.*t35.*t44.*(1.17e2./1.0e3))-q2d2.*t700-q2d4.*t702-q2d3.*t706+qd6.*(t10.*t30.*t43.*(1.17e2./1.0e3)-t10.*t44.*t163)+qd5.*(t10.*t44.*t47.*(1.17e2./1.0e3)+t10.*t43.*t165)+t10.*(t53-t63).*(t161+t162-t194-t195)+t10.*t30.*(t299+t300)-q2d6.*t10.*t44.*(1.17e2./1.0e3)-t10.*t47.*t334-t7.*t43.*t402-t10.*t44.*t75.*t163,-t597+t674+t10.*t74.*(t143+t144-t249-t250)+q2d2.*t10.*(-t243+t305+t306)-q2d5.*t10.*t44.*(1.17e2./1.0e3)-q2d3.*t10.*t295-q2d1.*t10.*t308-q2d4.*t10.*t309-qd6.*t10.*t200-qd3.*t10.*t292-qd5.*t10.*t301-qd4.*t10.*t317-qd2.*t10.*t324+t10.*t30.*t200,q2d5.*(t714+t715)-qd2.*(t200.*t338-t360.*(t146+t147-t201-t202)+t10.*t125.*t320+t10.*t163.*t321-t10.*t308.*t324-t10.*t334.*t342)+qd5.*(t200.*t268-(t269-t43.*(t143+t144-t249-t250)).*(t146+t147-t201-t202)-t10.*t334.*(t296+t297)+t10.*t125.*t165+t10.*t301.*t308-t10.*(t115+t119-t149).*(t143+t144-t249-t250))-q2d2.*t411-q2d3.*t591-q2d4.*t668+q2d1.*(t10.*t308.^2+t10.*t326.^2+t325.^2)+qd3.*(t164.*t264-t200.*t261-t10.*t287.*(t123+t124-t244)+t10.*t292.*t308+t10.*t290.*t334-t10.*(t272+t273-t336).*(t143+t144-t249-t250))-qd4.*(t200.*t275-t164.*t344+t10.*t125.*t314+t10.*t163.*t310-t10.*t308.*t317-t10.*t334.*t347)+qd6.*(t164.*t197-t200.*t308-t10.*t334.*(t146+t147-t201-t202)+t10.*t200.*t308),t585+t606-qd4.*(t200.*t373+t256.*t344-t10.*t303.*t314+t10.*t317.*t329+t10.*t334.*t394-t7.*t10.*t163.*t229)-qd3.*(t256.*t264+t200.*t386-t10.*t287.*t303+t10.*t292.*t329+t10.*t334.*t407-t7.*t10.*t163.*t365)+t579.*(t44.*t440+t27.*t43.*t442)-q2d1.*t411+q2d3.*t588+q2d4.*t665-q2d5.*t709+t200.*t360+q2d2.*(t10.*t303.^2+t10.*t404.^2+t256.^2)+qd5.*(t256.*t270+t200.*t383-t10.*t303.*(t161+t162-t194-t195)-t10.*(t299+t300).*(-t243+t305+t306)+t10.*t163.*t234+t10.*t334.*t390)+qd6.*(t200.*t329-t256.*t334-t10.*t200.*(-t243+t305+t306)+t10.*t256.*t334)-qd2.*(t256.*t360-t10.*t303.*t320+t10.*t324.*t329)+t10.*t598.*(t43.*t440-t27.*t44.*t442)-t10.*t163.*t320-t10.*t324.*t334-t10.*t436.*t600+t10.*t33.*t402.*t442,t585+t606+qd5.*(t200.*t549+t248.*(t269-t43.*(t143+t144-t249-t250))+t10.*t241.*(t143+t144-t249-t250)+t10.*t295.*(t299+t300)-t10.*t165.*t293+t10.*t334.*t563)-t579.*(t44.*t594+t27.*t43.*t596)-qd6.*(t200.*t295+t248.*t334-t10.*t200.*t295-t10.*t248.*t334)+q2d2.*t588-q2d1.*t591+q2d4.*t672-q2d5.*t711+t200.*t264+q2d3.*(t10.*t293.^2+t10.*t295.^2+t248.^2)-qd4.*(t248.*t344+t200.*t547-t10.*t293.*t314-t10.*t295.*t317+t10.*t334.*t560+t2.*t7.*t8.*t10.*t163.*(5.71e2./1.0e3))+qd3.*(-t248.*t264+t10.*t287.*t293+t10.*t292.*t295)+qd2.*(-t248.*t360+t10.*t293.*t320+t10.*t295.*t324)-t10.*t598.*(t43.*t594-t27.*t44.*t596)-t10.*t163.*t287-t10.*t292.*t334-t10.*t600.*t602-t10.*t33.*t402.*t596,t585+t606-t579.*(t44.*t429+t27.*t43.*t673)-qd6.*(t200.*t309+t266.*t334-t10.*t200.*t309-t10.*t266.*t334)+q2d2.*t665-q2d1.*t668+q2d3.*t672-q2d5.*t713+t200.*t344+qd5.*(t200.*t645+t266.*(t269-t43.*(t143+t144-t249-t250))+t10.*t27.*(t143+t144-t249-t250).*(3.0./2.5e1)-t7.*t10.*t165.*(3.0./2.5e1)+t10.*t334.*t652+t10.*(t299+t300).*(t243-t330))+qd3.*(-t264.*t266+t7.*t10.*t287.*(3.0./2.5e1)+t10.*t292.*t309)+qd4.*(-t266.*t344+t7.*t10.*t314.*(3.0./2.5e1)+t10.*t309.*t317)+qd2.*(-t266.*t360+t7.*t10.*t320.*(3.0./2.5e1)+t10.*t309.*t324)+q2d4.*(t10.*t33.*(9.0./6.25e2)+t10.*t662.^2+t266.^2)-t10.*t598.*(t43.*t429-t27.*t44.*t673)-t10.*t163.*t314-t10.*t317.*t334-t10.*t600.*t602-t10.*t33.*t402.*t673,q2d1.*(t714+t715)+qd3.*(t43.*t264.*(1.17e2./1.0e3)+t10.*t44.*t292.*(1.17e2./1.0e3))-qd5.*(t43.*t270.*(1.17e2./1.0e3)-t10.*t44.*t301.*(1.17e2./1.0e3))+qd4.*(t43.*t344.*(1.17e2./1.0e3)+t10.*t44.*t317.*(1.17e2./1.0e3))+qd2.*(t43.*t360.*(1.17e2./1.0e3)+t10.*t44.*t324.*(1.17e2./1.0e3))-qd6.*(t44.*t200.*(1.17e2./1.0e3)-t43.*t334.*(1.17e2./1.0e3)-t10.*t44.*t200.*(1.17e2./1.0e3)+t10.*t43.*t334.*(1.17e2./1.0e3))-q2d2.*t709-q2d3.*t711-q2d4.*t713-t200.*t270+q2d5.*(t357.*1.3689e-2+t10.*t355.*1.3689e-2)+t43.*t716.*(t577+t578)+t10.*(t161+t162-t194-t195).*(t143+t144-t249-t250)-t10.*t301.*t334+t7.*t10.*t402.*t430-t7.*t43.*t402.*t583-t10.*t27.*t402.*t605-t10.*t44.*t598.*t716-t7.*t10.*t44.*t402.*t600,t600.*(t577+t578)+t200.*t334-t583.*t598-t10.*t200.*t334-t10.*t579.*t600+t10.*t583.*(t597-t674)],[6,10]);


function [E tempprot] = RemoveNegMeas(Epn, protocol)
% Removes negative or zero measurements from a set and returns a protocol
% with the corresponding elements removed to exclude the negative
% measurements from the fitting.
%
% Epn is the full set of measurements; E is that with the negative ones
% removed.
%
% protocol is the full protocol; tempprot is that with the entries
% corresponding to negative measurements removed.
%
% author: Daniel C Alexander (d.alexander@ucl.ac.uk)
%         Gary Hui Zhang (gary.zhang@ucl.ac.uk)
%

tempprot = protocol;
E = Epn;
if(min(Epn)<=0)
    posonly = find(Epn>0);
    E = Epn(posonly);
    nonposonly = setdiff([1:size(Epn,1)], posonly);
    tempprot.totalmeas = length(posonly);
    % find the b0 indices for the new protocol without the non-positive
    % measurements
    tempprot.b0_Indices = [];
    for i=1:length(protocol.b0_Indices)
        currentB = protocol.b0_Indices(i);
        index = find(nonposonly<=currentB, 1, 'last');
        if isempty(index)
            tempprot.b0_Indices = [tempprot.b0_Indices currentB];
        elseif (nonposonly(index) ~= currentB)
            currentB = currentB - index;
            tempprot.b0_Indices = [tempprot.b0_Indices currentB];
        end
    end
    tempprot.numZeros = length(tempprot.b0_Indices);
    if(strcmp(tempprot.pulseseq, 'PGSE') || strcmp(tempprot.pulseseq, 'STEAM'))
        tempprot.G = tempprot.G(posonly);
        tempprot.delta = tempprot.delta(posonly);
        tempprot.smalldel = tempprot.smalldel(posonly);
        tempprot.grad_dirs = tempprot.grad_dirs(posonly, :);
    else
        error('Need to adapt for other pulse sequences.');
    end
end

if length(tempprot.b0_Indices) == 0
    error('All b=0 measurements are negative');
end


function [Eft, Varft, lpyt, Eyt, Varyt] = gpmc_loopred(gp, x, y, varargin)
%GPMC_LOOPRED  Leave-one-out predictions with Gaussian Process MCMC approximation.
%
%  Description
%    [EFT, VARFT, LPYT, EYT, VARYT] = GPMC_LOOPRED(RECGP, X, Y, OPTIONS)
%    takes a Gaussian processes record structure RECGP (returned by
%    gp_mc) together with a matrix X of training inputs and vector
%    Y of training targets, and evaluates the leave-one-out
%    predictive distribution at inputs X and returns means EFT and
%    variances VARFT of latent variables, the logarithm of the
%    predictive densities PYT, and the predictive means EYT and
%    variances VARYT of observations at input locations X.
%
%    OPTIONS is optional parameter-value pair
%      z      - optional observed quantity in triplet (x_i,y_i,z_i)
%               Some likelihoods may use this. For example, in case of
%               Poisson likelihood we have z_i=E_i, that is, expected value
%               for ith case.
%      is     - defines if importance sampling weighting is used 'on'
%               (default). If set to 'off', MCMC samples from the
%               full data posterior are used.
%
%    Given Gaussian likelihood or non-Gaussian likelihood and
%    latent method Laplace or EP, LOO-posterior given
%    hyperparameters is computed analytically or with analytic
%    approximation and LOO-posterior of the hyperparameters is
%    approximated using importance sampling. Optionally full data
%    posterior for hyperparameters can be used (by setting option
%    'is' to 'off'). Given non-Gaussian likelihood and latent
%    method MCMC, LOO-posterior of the hyperparameters and latent
%    values is approximated using importance sampling.
%
%  References:
%    Aki Vehtari and Jouko Lampinen (2002). Bayesian model
%    assessment and comparison using cross-validation predictive
%    densities. Neural Computation, 14(10):2439-2468.
%
%  See also
%   GP_LOOPRED, GP_MC, GP_PRED
%

% Copyright (c) 2012 Aki Vehtari

% This software is distributed under the GNU General Public
% License (version 3 or later); please refer to the file
% License.txt, included with the software, for details.

ip=inputParser;
ip.FunctionName = 'GPMC_LOOPRED';
ip.addRequired('gp',@isstruct);
ip.addRequired('x', @(x) ~isempty(x) && isreal(x) && all(isfinite(x(:))))
ip.addRequired('y', @(x) ~isempty(x) && isreal(x) && all(isfinite(x(:))))
ip.addParamValue('z', [], @(x) isreal(x) && all(isfinite(x(:))))
ip.addParamValue('is', 'on', @(x) ismember(x,{'on' 'off'}))
ip.parse(gp, x, y, varargin{:});
z=ip.Results.z;
is=ip.Results.is;

if isfield(gp,'meanf') && ~isempty(gp.meanf)
  error('GPMC_LOOPRED: Mean functions not yet supported');
end

nmc=size(gp.jitterSigma2,1);
[n,nin]=size(x);

if strcmp(gp.type, 'PIC_BLOCK') || strcmp(gp.type, 'PIC')
  ind = gp.tr_index;           % block indeces for training points
  gp = rmfield(gp,'tr_index');
end

for i1=1:nmc
  % compute leave-one-out predictions for each hyperparameter sample
  % if latent method is MCMC, then these samples are for latent values, too
  Gp = take_nth(gp,i1);
  switch gp.type
    case 'FULL'

    case {'FIC' 'CS+FIC'}
      % Reformat the inducing inputs
      u = reshape(Gp.X_u,length(Gp.X_u)/nin,nin);
      Gp.X_u = u;

    case {'PIC' 'PIC_BLOCK'}
      % Reformat the inducing inputs
      u = reshape(Gp.X_u,length(Gp.X_u)/nin,nin);
      Gp.X_u = u;
      Gp.tr_index = ind;
  end
  if nargout <= 3
    [Efts(:,i1), Varfts(:,i1), lpyts(:,i1)] = gp_loopred(Gp, x, y, 'z', z);
  else
    [Efts(:,i1), Varfts(:,i1), lpyts(:,i1), Eyts(:,i1), Varyts(:,i1)] = gp_loopred(Gp, x, y, 'z', z);
  end
end

if isequal(is,'off')
  w=1/nmc;
  lw=-log(nmc);
else
  % log importance sampling weights
  lw=-lpyts;
  % normalize weights
  for i2=1:n
    % this works even when lw have large magnitudes
    lw(i2,:)=lw(i2,:)-sumlogs(lw(i2,:));
  end
  % importance sampling weights
  w=exp(lw);
  % check the effective sample size
  m_eff=1./sum(w.^2,2);
  if min(m_eff)<n/10
    mn=sum(m_eff<(n/10));
    if mn==1
      warning(sprintf('For %d data point the effective sample size in IS is less than n/10',mn))
    else
      warning(sprintf('For %d data points the effective sample size in IS is less than n/10',mn))
    end
  end
end

% IS-LOO predictions
Eft = sum(Efts.*w, 2);
Varft = sum(Varfts.*w,2) + sum(bsxfun(@minus,Efts,Eft).^2.*w,2);
if nargout > 2
  lpyt = log(sum(exp(lpyts+lw),2)); % same as lpy=log(sum(exp(lpys).*w,2));
  if nargout > 3
    Eyt = sum(Eyts.*w,2);
    Varyt = sum(Varyts.*w,2) + sum(bsxfun(@minus,Eyts,Eyt).^2.*w,2);
  end
end


function [xUpdate,LUpdate,PUpdate,innov,Pzz,W]=separatedCovUpdateWithPred(z,R,zPred,PzPred,otherInfo,LPred,c)
%%SEPARATEDCOVUPDATEWITHPRED Given the output of the measurement prediction
%           step from separatedCovMeasPred and a measurement, complete the
%           measurement update step of the separated covariance filter.
%           Separating the measurement prediction step from the rest of the
%           update step can make the creation of multiple measurement
%           association hypotheses from a single target prediction more
%           efficient. The full measurement update function is
%           separatedCovUpdate.
%
%INPUTS: z The zDimX1 vector measurement.
%        R The zDimXzDim measurement covariance matrix in the native
%          coordinate system of the measurement.
%    zPred The zDimXnumComp measurement predictions from the filter.
%   PzPred The zDimXzDimXnumComp covariance matrices associated with zPred.
% otherInfo The intermediate results returned in the otherInfo output of
%          the separatedCovMeasPred function.
%    LPred The xDimXzDimXnumComp predicted delay vectors (defined before
%          Equation 12 in [1]). The use of multiple columns represents a
%          choice in how the algorithm was generalized to multiple
%          dimensions.
%        c The confidence region under consideration by the filter. 0<c<1.
%          If this parameter is omitted or an empty matrix is passed, the
%          default value of c=0.99 is used.
%
%OUTPUTS: xUpdate The xDimXnumComp updated state vectors.
%         LUpdate The xDimXzDimXnumComp updated delay vectors.
%         PUpdate The xDimXxDimXnumComp covariance matrices of the state
%                 estimates. This is a combination of LUpdate and PUpdate.
%      innov, Pzz The zDimXnumComp innovations and a zDimXzDimXnumComp
%                 set of matrices Pzz that are akin to an innovation
%                 covariance matrices are returned in case one wishes to
%                 analyze the consistency of the estimator or use those
%                 values in gating or likelihood evaluation.
%               W The xDimXzDimXnumComp gains used in the update.
%
%See the comments to the function separatedCovMeasPred for an example of
%usage of this function. See the comments to separatedCovUpdate for more
%information on the algorithm.
%
%REFERENCES:
%[1] G. J. Portmann, J. R. Moore, and W. G. Bath, "Separated covariance
%    filtering," in Proceedings of the IEEE International Radar Conference,
%    Arlington, VA, 7-10 May 1990, pp. 456-460.
%
%June 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<7||isempty(c))
    c=0.99;
end

H=otherInfo.H;
TPred=otherInfo.TPred;
xPred=otherInfo.xPred;
Pxz=otherInfo.Pxz;

xDim=size(xPred,1);
numComp=size(xPred,2);
zDim=size(z,1);

xUpdate=zeros(xDim,numComp);
LUpdate=zeros(xDim,zDim,numComp);
PUpdate=zeros(xDim,xDim,numComp);
innov=zeros(zDim,numComp);
Pzz=zeros(zDim,zDim,numComp);
W=zeros(xDim,zDim,numComp);
for k=1:numComp
    Pzz(:,:,k)=PzPred(:,:,k)+c^2*R;
    W(:,:,k)=Pxz(:,:,k)/Pzz(:,:,k);%The gain

    innov(:,k)=z-zPred(:,k);%The innovation
    xUpdate(:,k)=xPred(:,k)+W(:,:,k)*innov(:,k);

    xDim=size(xPred,1);
    diff=eye(xDim,xDim)-W(:,:,k)*H;

    LUpdate(:,:,k)=diff*LPred(:,:,k);
    TUpdate=diff*TPred(:,:,k);

    PUpdate(:,:,k)=(TUpdate-LUpdate(:,:,k)*LUpdate(:,:,k)')/c^2;
end
end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function varargout = colorspace(Conversion,varargin)
%   COLORSPACE  Convert a color image between color representations.
%   B = COLORSPACE(S,A) converts the color representation of image A
%   where S is a string specifying the conversion.  S tells the
%   source and destination color spaces, S = 'dest<-src', or
%   alternatively, S = 'src->dest'.  Supported color spaces are
%
%     'RGB'              R'G'B' Red Green Blue (ITU-R BT.709 gamma-corrected)
%     'YPbPr'            Luma (ITU-R BT.601) + Chroma
%     'YCbCr'/'YCC'      Luma + Chroma ("digitized" version of Y'PbPr)
%     'YUV'              NTSC PAL Y'UV Luma + Chroma
%     'YIQ'              NTSC Y'IQ Luma + Chroma
%     'YDbDr'            SECAM Y'DbDr Luma + Chroma
%     'JPEGYCbCr'        JPEG-Y'CbCr Luma + Chroma
%     'HSV'/'HSB'        Hue Saturation Value/Brightness
%     'HSL'/'HLS'/'HSI'  Hue Saturation Luminance/Intensity
%     'XYZ'              CIE XYZ
%     'Lab'              CIE L*a*b* (CIELAB)
%     'Luv'              CIE L*u*v* (CIELUV)
%     'Lch'              CIE L*ch (CIELCH)
%
%  All conversions assume 2 degree observer and D65 illuminant.  Color
%  space names are case insensitive.  When R'G'B' is the source or
%  destination, it can be omitted. For example 'yuv<-' is short for
%  'yuv<-rgb'.
%
%  MATLAB uses two standard data formats for R'G'B': double data with
%  intensities in the range 0 to 1, and uint8 data with integer-valued
%  intensities from 0 to 255.  As MATLAB's native datatype, double data is
%  the natural choice, and the R'G'B' format used by colorspace.  However,
%  for memory and computational performance, some functions also operate
%  with uint8 R'G'B'.  Given uint8 R'G'B' color data, colorspace will
%  first cast it to double R'G'B' before processing.
%
%  If A is an Mx3 array, like a colormap, B will also have size Mx3.
%
%  [B1,B2,B3] = COLORSPACE(S,A) specifies separate output channels.
%  COLORSPACE(S,A1,A2,A3) specifies separate input channels.

%  Pascal Getreuer 2005-2006

%%% Input parsing %%%
if nargin < 2, error('Not enough input arguments.'); end
[SrcSpace,DestSpace] = parse(Conversion);

if nargin == 2
   Image = varargin{1};
elseif nargin >= 3
   Image = cat(3,varargin{:});
else
   error('Invalid number of input arguments.');
end

FlipDims = (size(Image,3) == 1);

if FlipDims, Image = permute(Image,[1,3,2]); end
if ~isa(Image,'double'), Image = double(Image)/255; end
if size(Image,3) ~= 3, error('Invalid input size.'); end

SrcT = gettransform(SrcSpace);
DestT = gettransform(DestSpace);

if ~ischar(SrcT) & ~ischar(DestT)
   % Both source and destination transforms are affine, so they
   % can be composed into one affine operation
   T = [DestT(:,1:3)*SrcT(:,1:3),DestT(:,1:3)*SrcT(:,4)+DestT(:,4)];
   Temp = zeros(size(Image));
   Temp(:,:,1) = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3) + T(10);
   Temp(:,:,2) = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3) + T(11);
   Temp(:,:,3) = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3) + T(12);
   Image = Temp;
elseif ~ischar(DestT)
   Image = rgb(Image,SrcSpace);
   Temp = zeros(size(Image));
   Temp(:,:,1) = DestT(1)*Image(:,:,1) + DestT(4)*Image(:,:,2) + DestT(7)*Image(:,:,3) + DestT(10);
   Temp(:,:,2) = DestT(2)*Image(:,:,1) + DestT(5)*Image(:,:,2) + DestT(8)*Image(:,:,3) + DestT(11);
   Temp(:,:,3) = DestT(3)*Image(:,:,1) + DestT(6)*Image(:,:,2) + DestT(9)*Image(:,:,3) + DestT(12);
   Image = Temp;
else
   Image = feval(DestT,Image,SrcSpace);
end

%%% Output format %%%
if nargout > 1
   varargout = {Image(:,:,1),Image(:,:,2),Image(:,:,3)};
else
   if FlipDims, Image = permute(Image,[1,3,2]); end
   varargout = {Image};
end

return;

function [SrcSpace,DestSpace] = parse(Str)
% Parse conversion argument

if isstr(Str)
   Str = lower(strrep(strrep(Str,'-',''),' ',''));
   k = find(Str == '>');

   if length(k) == 1         % Interpret the form 'src->dest'
      SrcSpace = Str(1:k-1);
      DestSpace = Str(k+1:end);
   else
      k = find(Str == '<');

      if length(k) == 1      % Interpret the form 'dest<-src'
         DestSpace = Str(1:k-1);
         SrcSpace = Str(k+1:end);
      else
         error(['Invalid conversion, ''',Str,'''.']);
      end
   end

   SrcSpace = alias(SrcSpace);
   DestSpace = alias(DestSpace);
else
   SrcSpace = 1;             % No source pre-transform
   DestSpace = Conversion;
   if any(size(Conversion) ~= 3), error('Transformation matrix must be 3x3.'); end
end
return;

function Space = alias(Space)
Space = strrep(Space,'cie','');

if isempty(Space)
   Space = 'rgb';
end

switch Space
case {'ycbcr','ycc'}
   Space = 'ycbcr';
case {'hsv','hsb'}
   Space = 'hsv';
case {'hsl','hsi','hls'}
   Space = 'hsl';
case {'rgb','yuv','yiq','ydbdr','ycbcr','jpegycbcr','xyz','lab','luv','lch'}
   return;
end
return;

function T = gettransform(Space)
% Get a colorspace transform: either a matrix describing an affine transform,
% or a string referring to a conversion subroutine
switch Space
case 'ypbpr'
   T = [0.299,0.587,0.114,0;-0.1687367,-0.331264,0.5,0;0.5,-0.418688,-0.081312,0];
case 'yuv'
   % R'G'B' to NTSC/PAL YUV
   % Wikipedia: http://en.wikipedia.org/wiki/YUV
   T = [0.299,0.587,0.114,0;-0.147,-0.289,0.436,0;0.615,-0.515,-0.100,0];
case 'ydbdr'
   % R'G'B' to SECAM YDbDr
   % Wikipedia: http://en.wikipedia.org/wiki/YDbDr
   T = [0.299,0.587,0.114,0;-0.450,-0.883,1.333,0;-1.333,1.116,0.217,0];
case 'yiq'
   % R'G'B' in [0,1] to NTSC YIQ in [0,1];[-0.595716,0.595716];[-0.522591,0.522591];
   % Wikipedia: http://en.wikipedia.org/wiki/YIQ
   T = [0.299,0.587,0.114,0;0.595716,-0.274453,-0.321263,0;0.211456,-0.522591,0.311135,0];
case 'ycbcr'
   % R'G'B' (range [0,1]) to ITU-R BRT.601 (CCIR 601) Y'CbCr
   % Wikipedia: http://en.wikipedia.org/wiki/YCbCr
   % Poynton, Equation 3, scaling of R'G'B to Y'PbPr conversion
   T = [65.481,128.553,24.966,16;-37.797,-74.203,112.0,128;112.0,-93.786,-18.214,128];
case 'jpegycbcr'
   % Wikipedia: http://en.wikipedia.org/wiki/YCbCr
   T = [0.299,0.587,0.114,0;-0.168736,-0.331264,0.5,0.5;0.5,-0.418688,-0.081312,0.5]*255;
case {'rgb','xyz','hsv','hsl','lab','luv','lch'}
   T = Space;
otherwise
   error(['Unknown color space, ''',Space,'''.']);
end
return;

function Image = rgb(Image,SrcSpace)
% Convert to Rec. 709 R'G'B' from 'SrcSpace'
switch SrcSpace
case 'rgb'
   return;
case 'hsv'
   % Convert HSV to R'G'B'
   Image = huetorgb((1 - Image(:,:,2)).*Image(:,:,3),Image(:,:,3),Image(:,:,1));
case 'hsl'
   % Convert HSL to R'G'B'
   L = Image(:,:,3);
   Delta = Image(:,:,2).*min(L,1-L);
   Image = huetorgb(L-Delta,L+Delta,Image(:,:,1));
case {'xyz','lab','luv','lch'}
   % Convert to CIE XYZ
   Image = xyz(Image,SrcSpace);
   % Convert XYZ to RGB
   T = [3.240479,-1.53715,-0.498535;-0.969256,1.875992,0.041556;0.055648,-0.204043,1.057311];
   R = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3);  % R
   G = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3);  % G
   B = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3);  % B
   % Desaturate and rescale to constrain resulting RGB values to [0,1]
   AddWhite = -min(min(min(R,G),B),0);
   Scale = max(max(max(R,G),B)+AddWhite,1);
   R = (R + AddWhite)./Scale;
   G = (G + AddWhite)./Scale;
   B = (B + AddWhite)./Scale;
   % Apply gamma correction to convert RGB to Rec. 709 R'G'B'
   Image(:,:,1) = gammacorrection(R);  % R'
   Image(:,:,2) = gammacorrection(G);  % G'
   Image(:,:,3) = gammacorrection(B);  % B'
otherwise  % Conversion is through an affine transform
   T = gettransform(SrcSpace);
   temp = inv(T(:,1:3));
   T = [temp,-temp*T(:,4)];
   R = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3) + T(10);
   G = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3) + T(11);
   B = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3) + T(12);
   AddWhite = -min(min(min(R,G),B),0);
   Scale = max(max(max(R,G),B)+AddWhite,1);
   R = (R + AddWhite)./Scale;
   G = (G + AddWhite)./Scale;
   B = (B + AddWhite)./Scale;
   Image(:,:,1) = R;
   Image(:,:,2) = G;
   Image(:,:,3) = B;
end

% Clip to [0,1]
Image = min(max(Image,0),1);
return;

function Image = xyz(Image,SrcSpace)
% Convert to CIE XYZ from 'SrcSpace'
WhitePoint = [0.950456,1,1.088754];

switch SrcSpace
case 'xyz'
   return;
case 'luv'
   % Convert CIE L*uv to XYZ
   WhitePointU = (4*WhitePoint(1))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
   WhitePointV = (9*WhitePoint(2))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
   L = Image(:,:,1);
   Y = (L + 16)/116;
   Y = invf(Y)*WhitePoint(2);
   U = Image(:,:,2)./(13*L + 1e-6*(L==0)) + WhitePointU;
   V = Image(:,:,3)./(13*L + 1e-6*(L==0)) + WhitePointV;
   Image(:,:,1) = -(9*Y.*U)./((U-4).*V - U.*V);                  % X
   Image(:,:,2) = Y;                                             % Y
   Image(:,:,3) = (9*Y - (15*V.*Y) - (V.*Image(:,:,1)))./(3*V);  % Z
case {'lab','lch'}
   Image = lab(Image,SrcSpace);
   % Convert CIE L*ab to XYZ
   fY = (Image(:,:,1) + 16)/116;
   fX = fY + Image(:,:,2)/500;
   fZ = fY - Image(:,:,3)/200;
   Image(:,:,1) = WhitePoint(1)*invf(fX);  % X
   Image(:,:,2) = WhitePoint(2)*invf(fY);  % Y
   Image(:,:,3) = WhitePoint(3)*invf(fZ);  % Z
otherwise   % Convert from some gamma-corrected space
   % Convert to Rec. 701 R'G'B'
   Image = rgb(Image,SrcSpace);
   % Undo gamma correction
   R = invgammacorrection(Image(:,:,1));
   G = invgammacorrection(Image(:,:,2));
   B = invgammacorrection(Image(:,:,3));
   % Convert RGB to XYZ
   T = inv([3.240479,-1.53715,-0.498535;-0.969256,1.875992,0.041556;0.055648,-0.204043,1.057311]);
   Image(:,:,1) = T(1)*R + T(4)*G + T(7)*B;  % X
   Image(:,:,2) = T(2)*R + T(5)*G + T(8)*B;  % Y
   Image(:,:,3) = T(3)*R + T(6)*G + T(9)*B;  % Z
end
return;

function Image = hsv(Image,SrcSpace)
% Convert to HSV
Image = rgb(Image,SrcSpace);
V = max(Image,[],3);
S = (V - min(Image,[],3))./(V + (V == 0));
Image(:,:,1) = rgbtohue(Image);
Image(:,:,2) = S;
Image(:,:,3) = V;
return;

function Image = hsl(Image,SrcSpace)
% Convert to HSL
switch SrcSpace
case 'hsv'
   % Convert HSV to HSL
   MaxVal = Image(:,:,3);
   MinVal = (1 - Image(:,:,2)).*MaxVal;
   L = 0.5*(MaxVal + MinVal);
   temp = min(L,1-L);
   Image(:,:,2) = 0.5*(MaxVal - MinVal)./(temp + (temp == 0));
   Image(:,:,3) = L;
otherwise
   Image = rgb(Image,SrcSpace);  % Convert to Rec. 701 R'G'B'
   % Convert R'G'B' to HSL
   MinVal = min(Image,[],3);
   MaxVal = max(Image,[],3);
   L = 0.5*(MaxVal + MinVal);
   temp = min(L,1-L);
   S = 0.5*(MaxVal - MinVal)./(temp + (temp == 0));
   Image(:,:,1) = rgbtohue(Image);
   Image(:,:,2) = S;
   Image(:,:,3) = L;
end
return;

function Image = lab(Image,SrcSpace)
% Convert to CIE L*a*b* (CIELAB)
WhitePoint = [0.950456,1,1.088754];

switch SrcSpace
case 'lab'
   return;
case 'lch'
   % Convert CIE L*CH to CIE L*ab
   C = Image(:,:,2);
   Image(:,:,2) = cos(Image(:,:,3)*pi/180).*C;  % a*
   Image(:,:,3) = sin(Image(:,:,3)*pi/180).*C;  % b*
otherwise
   Image = xyz(Image,SrcSpace);  % Convert to XYZ
   % Convert XYZ to CIE L*a*b*
   X = Image(:,:,1)/WhitePoint(1);
   Y = Image(:,:,2)/WhitePoint(2);
   Z = Image(:,:,3)/WhitePoint(3);
   fX = f(X);
   fY = f(Y);
   fZ = f(Z);
   Image(:,:,1) = 116*fY - 16;    % L*
   Image(:,:,2) = 500*(fX - fY);  % a*
   Image(:,:,3) = 200*(fY - fZ);  % b*
end
return;

function Image = luv(Image,SrcSpace)
% Convert to CIE L*u*v* (CIELUV)
WhitePoint = [0.950456,1,1.088754];
WhitePointU = (4*WhitePoint(1))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
WhitePointV = (9*WhitePoint(2))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));

Image = xyz(Image,SrcSpace); % Convert to XYZ
U = (4*Image(:,:,1))./(Image(:,:,1) + 15*Image(:,:,2) + 3*Image(:,:,3));
V = (9*Image(:,:,2))./(Image(:,:,1) + 15*Image(:,:,2) + 3*Image(:,:,3));
Y = Image(:,:,2)/WhitePoint(2);
L = 116*f(Y) - 16;
Image(:,:,1) = L;                        % L*
Image(:,:,2) = 13*L.*(U - WhitePointU);  % u*
Image(:,:,3) = 13*L.*(V - WhitePointV);  % v*
return;

function Image = lch(Image,SrcSpace)
% Convert to CIE L*ch
Image = lab(Image,SrcSpace);  % Convert to CIE L*ab
H = atan2(Image(:,:,3),Image(:,:,2));
H = H*180/pi + 360*(H < 0);
Image(:,:,2) = sqrt(Image(:,:,2).^2 + Image(:,:,3).^2);  % C
Image(:,:,3) = H;                                        % H
return;

function Image = huetorgb(m0,m2,H)
% Convert HSV or HSL hue to RGB
N = size(H);
H = min(max(H(:),0),360)/60;
m0 = m0(:);
m2 = m2(:);
F = H - round(H/2)*2;
M = [m0, m0 + (m2-m0).*abs(F), m2];
Num = length(m0);
j = [2 1 0;1 2 0;0 2 1;0 1 2;1 0 2;2 0 1;2 1 0]*Num;
k = floor(H) + 1;
Image = reshape([M(j(k,1)+(1:Num).'),M(j(k,2)+(1:Num).'),M(j(k,3)+(1:Num).')],[N,3]);
return;

function H = rgbtohue(Image)
% Convert RGB to HSV or HSL hue
[M,i] = sort(Image,3);
i = i(:,:,3);
Delta = M(:,:,3) - M(:,:,1);
Delta = Delta + (Delta == 0);
R = Image(:,:,1);
G = Image(:,:,2);
B = Image(:,:,3);
H = zeros(size(R));
k = (i == 1);
H(k) = (G(k) - B(k))./Delta(k);
k = (i == 2);
H(k) = 2 + (B(k) - R(k))./Delta(k);
k = (i == 3);
H(k) = 4 + (R(k) - G(k))./Delta(k);
H = 60*H + 360*(H < 0);
H(Delta == 0) = nan;
return;

function Rp = gammacorrection(R)
Rp = real(1.099*R.^0.45 - 0.099);
i = (R < 0.018);
Rp(i) = 4.5138*R(i);
return;

function R = invgammacorrection(Rp)
R = real(((Rp + 0.099)/1.099).^(1/0.45));
i = (R < 0.018);
R(i) = Rp(i)/4.5138;
return;

function fY = f(Y)
fY = real(Y.^(1/3));
i = (Y < 0.008856);
fY(i) = Y(i)*(841/108) + (4/29);
return;

function Y = invf(fY)
Y = fY.^3;
i = (Y < 0.008856);
Y(i) = (fY(i) - 4/29)*(108/841);
return;


function z = mtimes( x, y, oper )

%   Disciplined convex programming information for MTIMES:
%      True matrix multiplications Z = X * Y---that is, where neither X
%      nor Y is a scalar---require both multiplications and additions.
%      For example, element (i,j) of Z is given by
%         X(i,1) * Y(1,j) + X(i,2) * Y(2,j) + ... + X(i,k) * Y(k,j)
%      Therefore, matrix multiplications must satisfy *both* the
%      "no-product" rule of multiplication and the "same curvature" rule
%      of addition. See the help for CVX/TIMES and CVX/PLUS,
%      respectively, for individual descriptions of these rules.
%
%      An exception is made to these general rules for quadratic forms.
%      That is, two affine expressions may be multiplied together if the
%      result can immediately be verified as a convex quadratic form.
%      For example, the construction
%         variable x(n)
%         x' * Q * x  <= 1;
%      would be permitted if Q is *constant* and positive semidefinite.
%
%   Disciplined geometric programming information for TIMES:
%      As mentioned above, true matrix multiplies Z = X * Y require both
%      multiplications and additions. Since only log-convex terms can be
%      summed, both X and Y must be elementwise log-convex/affine.

persistent remap
if nargin < 3, oper = 'times'; end

%
% Check sizes
%

sx = size( x );
sy = size( y );
if all( sx == 1 ) || all( sy == 1 ),
    z = feval( oper, x, y );
    return
elseif length( sx ) > 2 || length( sy ) > 2,
    error( 'Input arguments must be 2-D.' );
elseif sx( 2 ) ~= sy( 1 ),
    error( 'Inner matrix dimensions must agree.' );
else
    sz = [ sx( 1 ), sy( 2 ) ];
end
nz = prod( sz );

%
% Check expression types
%

if cvx_isconstant( x ),

    xC = cvx_constant( x );
    if nnz( isnan( xC ) ),
        error( 'Disciplined convex programming error:\n    Invalid numeric values (NaNs) may not be used in CVX expressions.', 1 ); %#ok
    elseif cvx_isconstant( y ),
        yC = cvx_constant( y );
        if nnz( isnan( yC ) ),
            error( 'Disciplined convex programming error:\n    Invalid numeric values (NaNs) may not be used in CVX expressions.', 1 ); %#ok
        end
        z = feval( [ 'm', oper ], xC, yC );
        if nnz( isnan( z ) ),
            error( 'Disciplined convex programming error:\n    This expression produced one or more invalid numeric values (NaNs).', 1 ); %#ok
        end
        z = cvx( z );
        return
    elseif isequal( oper, 'rdivide' ),
        error( 'Disciplined convex programming error:\n    Matrix divisor must be constant.', 1 ); %#ok
    end
    yA   = cvx_basis( y );
    laff = true;
    cnst = false;
    raff = false;
    quad = false;
    posy = false;
    vpos = false;

elseif cvx_isconstant( y ),

    yC = cvx_constant( y );
    if nnz( isnan( yC ) ),
        error( 'Disciplined convex programming error:\n    Invalid numeric values (NaNs) may not be used in CVX expressions.', 1 ); %#ok
    elseif isequal( oper, 'ldivide' ),
        error( 'Disciplined convex programming error:\n    Matrix divisor must be constant.', 1 ); %#ok
    end
    xA   = cvx_basis( x );
    raff = true;
    laff = false;
    quad = false;
    cnst = false;
    posy = false;
    vpos = false;

else

    if isempty( remap ),
        remap_0 = cvx_remap( 'zero' );
        remap_1 = cvx_remap( 'nonzero', 'complex' );
        temp    = ~( remap_0 | remap_1 );
        remap_2 = cvx_remap( 'affine' ) & temp;
        remap_4 = cvx_remap( 'log-convex' );
        remap_5 = cvx_remap( 'log-concave' ) & ~remap_4;
        remap_4 = remap_4 & temp;
        remap_3 = cvx_remap( 'valid' ) & ~( remap_0 | remap_1 | remap_2 | remap_4 | remap_5 );
        remap   = remap_1 + 2 * remap_2 + 3 * remap_3 + 4 * remap_4 + 5 * remap_5 - cvx_remap( 'invalid' );
    end
    vx = remap( cvx_classify( x ) );
    vy = remap( cvx_classify( y ) );
    xA = cvx_basis( x );
    yA = cvx_basis( y );
    xC = cvx_reshape( xA( 1, : ), sx );
    yC = cvx_reshape( yA( 1, : ), sy );
    vx = reshape( vx, sx );
    vy = reshape( vy, sy );
    cx = xC ~= 0;
    cy = yC ~= 0;
    ax = vx == 2;
    ay = vy == 2;
    px = vx == 4;
    py = vy == 4;
    gx = vx == 5;
    gy = vy == 5;
    quad = +ax * +ay;
    if nnz( quad ) ~= 0,
        if length( quad ) ~= 1,
            error( 'Disciplined convex programming error:\n    Only scalar quadratic forms can be specified in CVX\n.', 1 ); %#ok
        else
            cx = cx & ~ax;
            cy = cy & ~ay;
            xC( ax ) = 0;
            yC( ay ) = 0;
        end
    end
    cnst = +cx * +cy; %#ok
    laff = +cx * +( vy > 1 );
    raff = +( vx > 1 ) * cy;
    posy = +px * +py;
    vpos = +gx * +gy;
    if nnz( raff ) ~= 0,
        raff = true;
        cnst = false;
    elseif nnz( laff ) ~= 0,
        laff = true;
        cnst = false;
    else
        laff = false;
        raff = false;
        cnst = true;
    end
    othr = +( vx > 1 | vx < 0 ) * +( vy > 1 | vy < 0 ) - quad - posy - vpos;
    if nnz( othr ) ~= 0,
        error( 'Disciplined convex programming error:\n    Cannot perform the operation {%s}*{%s}', cvx_class( x ), cvx_class( y ) );
    end
    quad = nnz( quad ) ~= 0;
    posy = nnz( posy ) ~= 0;

end

first = true;

if cnst,
    switch oper,
    case 'ldivide', z2 = xC \ yC;
    case 'rdivide', z2 = xC / yC;
    otherwise,      z2 = xC * yC;
    end
    if first, z = z2; first = false; else z = z + z2; end %#ok
end

if raff,
    % everything * constant
    nA = size( xA, 1 );
    z2 = cvx_reshape( xA, [ nA * sx( 1 ), sx( 2 ) ] );
    if issparse( z2 ),
        tt = any( z2, 2 );
        if cvx_use_sparse( size( z2 ), nnz( tt ) * sx( 2 ), isreal( z2 ) & isreal( yC ) ),
            z2 = z2( tt, : );
        else
            tt = [];
        end
    else
        tt = [];
    end
    switch oper,
    case 'rdivide', z2 = z2 / yC;
    otherwise,      z2 = z2 * yC;
    end
    z2 = cvx_reshape( z2, [ nA, nz ], tt );
    z2 = cvx( sz, z2 );
    if first, z = z2; first = false; else z = z + z2; end
end

if laff,
    % constant * everything
    nA = size( yA, 1 );
    t1 = reshape( 1 : prod( sy ), sy )';
    t2 = reshape( 1 : prod( sz ), [ sz(2), sz(1) ] )';
    z2 = yA; if raff, z2( 1, : ) = 0; end
    z2 = cvx_reshape( z2, [ nA * sy( 2 ), sy( 1 ) ], [], t1 );
    if issparse( z2 ),
        tt = any( z2, 2 );
        if cvx_use_sparse( size( z2 ), nnz( tt ) * sy( 1 ), isreal( z2 ) & isreal( xC ) ),
            z2 = z2( tt, : );
        else
            tt = [];
        end
    else
        tt = [];
    end
    switch oper,
    case 'ldivide', z2 = z2 / xC.';
    otherwise,      z2 = z2 * xC.';
    end
    z2 = cvx_reshape( z2, [ nA, nz ], tt, [], t2 );
    z2 = cvx( sz, z2 );
    if first, z = z2; first = false; else z = z + z2; end
end

if quad,
    % affine * affine
    tt = ax( : ) & ay( : );
    xA = xA( :, tt ); xB = xA( 1, : ); xA( 1, : ) = 0;
    yA = yA( :, tt ); yB = yA( 1, : ); yA( 1, : ) = 0;
    xM = size( xA, 1 ); yM = size( yA, 1 );
    if xM < yM, xA( yM, end ) = 0;
    elseif yM < xM, yA( xM, end ) = 0; end
    %
    % Quadratic form test 1: See if x == a conj( y ) + b for some real a, b,
    % so that the quadratic form involves a simple squaring (or sum of squares)
    %
    cyA   = conj( yA );
    alpha = sum( sum( real( xA .* yA ) ) ) ./ max( sum( sum( cyA .* yA ) ), realmin );
    if sum( sum( abs( xA - alpha * cyA ) ) ) <= 2 * eps * sum( sum( abs( xA ) ) ),
        beta = xB - alpha * conj( yB );
        yt = cvx( [ 1, size( yA, 2 ) ], yA ) + yB;
        if isreal( yA ) && isreal( yB ) && isreal( beta ),
            beta = ( 0.5 / alpha ) * beta;
            z2 = alpha * ( sum_square( yt + beta ) - sum_square( beta ) );
        elseif all( abs( beta ) <= 2 * eps * abs( xB ) ),
            z2 = alpha * sum_square_abs( yt );
        else
            error( 'Disciplined convex programming error:\n    Invalid quadratic form: product is not real.\n', 1 ); %#ok
        end
    else
        %
        % Quadratic form test 2: Extract the quadratic coefficient matrix
        % and test it for semidefiniteness
        %
        dx = find( any( xA, 2 ) | any( yA, 2 ) );
        zb = length( dx );
        cxA = conj( xA( dx, : ) );
        cyA = cyA( dx, : );
        P  = cxA * cyA.';
        Q  = cxA * yB.' + cyA * xB.';
        R  = xB * yB.';
        P  = 0.5 * ( P + P.' );
        if ~isreal( R ) || ~isreal( Q ) || ~isreal( P ),
            error( 'Disciplined convex programming error:\n   Invalid quadratic form: product is complex.', 1 ); %#ok
        else
            xx = cvx( zb, sparse( dx, 1 : zb, 1 ) );
            [ z2, success ] = quad_form( xx, P, Q, R );
            if ~success,
                error( 'Disciplined convex programming error:\n   Invalid quadratic form: neither convex nor concave.', 1 ); %#ok
            end
        end
    end
    if first, z = z2; first = false; else z = z + z2; end
end

if posy,
    [ ix, jx ] = find( reshape( px, sx ) );
    vx = log( cvx_subsref( x, px ) );
    [ iy, jy ] = find( reshape( py, sy ) );
    vy = log( cvx_subsref( y, py ) );
    [ iz, jz ] = find( sparse( 1 : nnz(px), jx, 1 ) * sparse( iy, 1 : nnz(py), 1 ) );
    z2 = exp( vec( cvx_subsref( vx, iz ) ) + vec( cvx_subsref( vy, jz ) ) );
    z2 = sparse( ix(iz), jy(jz), z2, sz(1), sz(2) );
    if first, z = z2; first = false; else z = z + z2; end
end

if vpos,
    [ ix, jx ] = find( reshape( gx, sx ) );
    vx = log( cvx_subsref( x, gx ) );
    [ iy, jy ] = find( reshape( gy, sy ) );
    vy = log( cvx_subsref( y, gy ) );
    [ iz, jz ] = find( sparse( 1 : nnz(gx), jx, 1 ) * sparse( iy, 1 : nnz(gy), 1 ) );
    z2 = exp( vec( cvx_subsref( vx, iz ) ) + vec( cvx_subsref( vy, jz ) ) );
    z2 = sparse( ix(iz), jy(jz), z2, sz(1), sz(2) );
    if first, z = z2; first = false; else z = z + z2; end %#ok
end

%
% Check that the sums are legal
%

v = cvx_vexity( z );
if any( isnan( v( : ) ) ),
    temp = 'Disciplined convex programming error:';
    tt = isnan( cvx_constant( z ) );
    if any( tt ),
        temp = [ temp, '\n    This expression produced one or more invalid numeric values (NaNs).' ];
    end
    if any( isnan( v( ~tt ) ) ),
        temp = [ temp, '\n   Illegal affine combination of convex and/or concave terms detected.' ];
    end
    error( temp, 1 );
end

% Copyright 2005-2016 CVX Research, Inc.
% See the file LICENSE.txt for full copyright information.
% The command 'cvx_where' will show where this file is located.


classdef AMICO_VERDICTPROSTATE

properties
    id, name                % id and name of the model
    max_dirs                % maximum number of directions to fit
    dIC                     %
    Rs                      %
    dEES                    %
    P                       %
    OUTPUT_names            % suffix of the output maps
    OUTPUT_descriptions     % description of the output maps
end

methods

    % =================================
    % Setup the parameters of the model
    % =================================
    function obj = AMICO_VERDICTPROSTATE()
        global CONFIG

        % set the parameters of the model
        obj.id        = 'VerdictProstate';
        obj.name      = 'VERDICT prostate';
        obj.max_dirs  = 0; % no need to estimate directions, it's an isotropic model
        obj.dIC       = 2.0 * 1E-3;
        obj.Rs        = linspace(0.01,20.1,20);
        obj.dEES      = 2.0 * 1E-3;
        obj.P         = 8.0 * 1E-3;
        obj.OUTPUT_names        = {'R', 'fIC', 'fEES', 'fVASC', 'Fobj'};
        obj.OUTPUT_descriptions = {'R', 'fIC', 'fEES', 'fVASC', 'Fobj'};

        % set the parameters to fit it
        CONFIG.OPTIMIZATION.SPAMS_param.mode    = 2;
        CONFIG.OPTIMIZATION.SPAMS_param.pos     = true;
        CONFIG.OPTIMIZATION.SPAMS_param.lambda  = 0;    % l1 regularization
        CONFIG.OPTIMIZATION.SPAMS_param.lambda2 = 1e-3; % l2 regularization
    end

    % ==================================================================
    % Generate high-resolution kernels and rotate them in harmonic space
    % ==================================================================
    function GenerateKernels( obj, ATOMS_path, schemeHR, AUX, idx_IN, idx_OUT )
        global CONFIG AMICO_data_path CAMINO_path

        % check if high-resolution scheme has been created
        schemeHrFilename = fullfile(ATOMS_path,'protocol_HR.scheme');
        if ~exist( schemeHrFilename, 'file' )
            error( '[AMICO_VERDICTPROSTATE.GenerateKernels] File "protocol_HR.scheme" not found in folder "%s"', ATOMS_path )
        end

        filenameHr = [tempname '.Bfloat'];
        progress = ProgressBar( numel(obj.Rs) + 2 );

        % IC compartment
        % ==============
        for R = obj.Rs
            % generate
            if exist( filenameHr, 'file' ), delete( filenameHr ); end
            CMD = sprintf( '%s/datasynth -synthmodel compartment 1 SPHEREGPD %E %E -schemefile %s -voxels 1 -outputfile %s 2> /dev/null', CAMINO_path, obj.dIC*1e-6, R*1e-6, schemeHrFilename, filenameHr );
            [status result] = system( CMD );
            if status>0
                disp(result)
                error( '[AMICO_VERDICTPROSTATE.GenerateKernels] Problems generating the signal with datasynth' );
            end

            % rotate and save
            fid = fopen( filenameHr, 'r', 'b' );
            signal = fread(fid,'float');
            fclose(fid);
            delete( filenameHr );
            lm = AMICO_RotateKernel( signal, AUX, idx_IN, idx_OUT, true );
            save( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), '-v6', 'lm' )
            progress.update();
        end

        % EES compartment
        % ===============
        % generate
        if exist( filenameHr, 'file' ), delete( filenameHr ); end
        CMD = sprintf( '%s/datasynth -synthmodel compartment 1 BALL %E -schemefile %s -voxels 1 -outputfile %s 2> /dev/null', CAMINO_path, obj.dEES*1e-6, schemeHrFilename, filenameHr );
        [status result] = system( CMD );
        if status>0
            disp(result)
            error( '[AMICO_VERDICTPROSTATE.GenerateKernels] problems generating the signal' );
        end

        % resample and save
        fid = fopen( filenameHr, 'r', 'b' );
        signal = fread(fid,'float');
        fclose(fid);
        delete( filenameHr );
        lm = AMICO_RotateKernel( signal, AUX, idx_IN, idx_OUT, true );
        save( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), '-v6', 'lm' )
        progress.update();

        % VASC compartment
        % ================
        % generate
        if exist( filenameHr, 'file' ), delete( filenameHr ); end
        CMD = sprintf( '%s/datasynth -synthmodel compartment 1 ASTROSTICKS %E -schemefile %s -voxels 1 -outputfile %s 2> /dev/null', CAMINO_path, obj.P*1e-6, schemeHrFilename, filenameHr );
        [status result] = system( CMD );
        if status>0
            disp(result)
            error( '[AMICO_VERDICTPROSTATE.GenerateKernels] problems generating the signal' );
        end

        % resample and save
        fid = fopen( filenameHr, 'r', 'b' );
        signal = fread(fid,'float');
        fclose(fid);
        delete( filenameHr );
        lm = AMICO_RotateKernel( signal, AUX, idx_IN, idx_OUT, true );
        save( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), '-v6', 'lm' )
        progress.update();

        progress.close();
end

    % ==============================================
    % Project kernels from harmonic to subject space
    % ==============================================
    function ResampleKernels( obj, ATOMS_path, idx_OUT, Ylm_OUT )
        global CONFIG AMICO_data_path KERNELS

        % Setup the KERNELS structure
        % ===========================
        nIC = numel(obj.Rs);

        KERNELS = {};
        KERNELS.model    = 'VERDICTPROSTATE';
        KERNELS.nS       = CONFIG.scheme.nS;
        KERNELS.nA       = nIC + 2; % number of atoms

        KERNELS.Aic      = zeros( [KERNELS.nS nIC], 'single' );
        KERNELS.Aic_R    = zeros( 1, nIC, 'single' );

        KERNELS.Aees      = zeros( [KERNELS.nS 1], 'single' );
        KERNELS.Aees_d    = NaN;

        KERNELS.Avasc     = zeros( [KERNELS.nS 1], 'single' );
        KERNELS.Avasc_P   = NaN;

        progress = ProgressBar( KERNELS.nA );

        % IC compartment
        % ==============
        for i = 1:nIC
            load( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), 'lm' );
            KERNELS.Aic(:,i) = AMICO_ResampleKernel( lm, idx_OUT, Ylm_OUT, true );
            KERNELS.Aic_R(i) = obj.Rs(i);
            progress.update();
        end

        % Precompute norms of coupled atoms (for the l1 minimization)
        A = double( KERNELS.Aic(CONFIG.scheme.dwi_idx,:) );
        KERNELS.Aic_norm = repmat( 1./sqrt( sum(A.^2) ), [size(A,1),1] );
        clear A

        % EES compartment
        % ===============
        load( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), 'lm' );
        KERNELS.Aees   = AMICO_ResampleKernel( lm, idx_OUT, Ylm_OUT, true );
        KERNELS.Aees_d = obj.dEES;
        progress.update();

        % VASC compartment
        % ================
        load( fullfile( ATOMS_path, sprintf('A_%03d.mat',progress.i) ), 'lm' );
        KERNELS.Avasc   = AMICO_ResampleKernel( lm, idx_OUT, Ylm_OUT, true );
        KERNELS.Avasc_P = obj.P;
        progress.update();

        progress.close();
    end

    % ===========================
    % Fit the model to each voxel
    % ===========================
    function [ MAPs ] = Fit( obj, y, i1, i2 )
        global CONFIG KERNELS

%         A = double( [ KERNELS.Aic(CONFIG.scheme.dwi_idx,:) KERNELS.Aees(CONFIG.scheme.dwi_idx) KERNELS.Avasc(CONFIG.scheme.dwi_idx) ] );
%         AA = [ ones(1,KERNELS.nA) ; A ];
%         yy = [ 1 ; y(CONFIG.scheme.dwi_idx) ];
        A = double( [ KERNELS.Aic KERNELS.Aees KERNELS.Avasc ] );
        AA = A;%[ ones(1,KERNELS.nA) ; A ];
        yy = y;%[ 1 ; y(CONFIG.scheme.dwi_idx) ];

        switch( 1 )

            case 1
            % [ normal fitting ]
            norms = repmat( 1./sqrt(sum(AA.^2)), [size(AA,1),1] );
            AA = AA .* norms;
            x = full( mexLasso( yy, AA, CONFIG.OPTIMIZATION.SPAMS_param ) );
            x = x .* norms(1,:)';

            case 2
            params = CONFIG.OPTIMIZATION.SPAMS_param;
            params.loss = 'square';
            params.regul = 'group-lasso-l2';
            params.groups = int32([ repmat(1,1,numel(KERNELS.Aic_R)) 2 3 ]);  % all the groups are of size 2
            params.intercept = false;
            x = full( mexFistaFlat( yy, AA, zeros(size(A,2),1), params ) );

            case 3
            % [NODDI style] estimate and remove EES and VASC contributions
            y = y(CONFIG.scheme.dwi_idx);
            x = lsqnonneg( AA, yy, CONFIG.OPTIMIZATION.LS_param );
            y = y - x(end)*A(:,end);
            y = y - x(end-1)*A(:,end-1);

            % find sparse support for remaining signal
            An = A(:,1:size(KERNELS.Aic,2)) .* KERNELS.Aic_norm;
            x = full( mexLasso( y, An, CONFIG.OPTIMIZATION.SPAMS_param ) );

            % debias coefficients
            idx = [ x>0 ; true ; true ];
            x(idx) = lsqnonneg( AA(:,idx), yy, CONFIG.OPTIMIZATION.LS_param );
        end

        % compute MAPS
        xIC = x( 1:end-2 );
        fIC = sum( xIC );
        MAPs(1) = KERNELS.Aic_R * xIC / ( fIC + eps );           % cell radius
        MAPs(2) = fIC;                                           % fIC
        MAPs(3) = x( end-1 );                                    % fEES
        MAPs(4) = x( end );                                      % fVASC

        y_predicted = A*x;
        MAPs(5) = norm(y-y_predicted) / norm(y);
%         sigma = 0;
%         MAPs(5) = sum( (y - sqrt((y_predicted).^2+sigma^2) ) ).^2;
    end

end

end