function [D,Aw]=method_2_prep(A,W)
% This function computes two arrays in preparation for
% method 2 (the real colors case), also called the
% "LLSS" (Least Log Slope Squared) method. (This code
% is the same as method_3_prep.)

% A is the nx3 matrix of color matching functions.
% W is an nx1 illuminant vector, scaled arbitrarily.
% D is an nxn matrix of finite differencing constants.
% Aw is an nx3 matrix of illuminant-W-referenced CMFs.

n=size(A,1);
D=full(gallery('tridiag',n,-2,4,-2));
D(1,1)=2; D(n,n)=2;
W_normalized=W/(A(:,2)'*W);
Aw=diag(W_normalized)*A;