(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



CG[A_,b_]:=Module[
{Tol=10^-10,n=Length[b],MaxIter=10^3,P,X,p,r,x,i=1},
P= ConstantArray[0,{MaxIter,n}];
X= ConstantArray[0,{MaxIter,n}];
(* Initializing the Conjugate Directions using b*)
r=b;
p=r/Sqrt[r.A.r];
x=(p.r)p;
{X[[i]],P[[i]]}={x,p};
While[
And[Norm[r]>Tol,i++<MaxIter],
(* Computing residual *)
r=b-A.x;
(* Conjugating *)
p=r;
Do[p=p-(p.A.P[[j]]) P[[j]],{j,1,i-1}];
(*Do[p=p-(p.A.P[[j]]) P[[j]],{j,Max[1,i-12,i-1}];*)
p=p/Sqrt[p.A.p];
(* Updating x *)
x+=(p.r)p;
(* Storing Data *)
{X[[i]],P[[i]]}={x,p}
];
i=Min[i,MaxIter];
{Take[X,i],Take[P,i]}
] 


RandomEvalSymPDMat[ EvalDist_]:= Module[
{evals,n,Q},
evals=Abs[Flatten[
Table[ 
RandomVariate[EvalDist[[i,1]],EvalDist[[i,2]]],
{i, Length[EvalDist]}]
]
];
n=Length[evals];
Q=QRDecomposition[RandomReal[{-1,1},{n,n}]][[1]];
Q.DiagonalMatrix[evals].Transpose[Q]
]



