/************************************************************************* * Unsets HQRNDState structure *************************************************************************/ private static void unsetstate(ref hqrnd.hqrndstate state) { state.s1 = 0; state.s2 = 0; state.v = 0; state.magicv = 0; }
/************************************************************************* * Generation of random NxN complex matrix with given condition number C and * norm2(A)=1 * * INPUT PARAMETERS: * N - matrix size * C - condition number (in 2-norm) * * OUTPUT PARAMETERS: * A - random matrix with norm2(A)=1 and cond(A)=C * * -- ALGLIB routine -- * 04.12.2009 * Bochkanov Sergey *************************************************************************/ public static void cmatrixrndcond(int n, double c, ref AP.Complex[,] a) { int i = 0; int j = 0; double l1 = 0; double l2 = 0; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); AP.Complex v = 0; System.Diagnostics.Debug.Assert(n >= 1 & (double)(c) >= (double)(1), "CMatrixRndCond: N<1 or C<1!"); a = new AP.Complex[n - 1 + 1, n - 1 + 1]; if (n == 1) { // // special case // hqrnd.hqrndrandomize(ref state); hqrnd.hqrndunit2(ref state, ref v.x, ref v.y); a[0, 0] = v; return; } l1 = 0; l2 = Math.Log(1 / c); for (i = 0; i <= n - 1; i++) { for (j = 0; j <= n - 1; j++) { a[i, j] = 0; } } a[0, 0] = Math.Exp(l1); for (i = 1; i <= n - 2; i++) { a[i, i] = Math.Exp(AP.Math.RandomReal() * (l2 - l1) + l1); } a[n - 1, n - 1] = Math.Exp(l2); cmatrixrndorthogonalfromtheleft(ref a, n, n); cmatrixrndorthogonalfromtheright(ref a, n, n); }
public static bool testhqrnd(bool silent) { bool result = new bool(); bool waserrors = new bool(); int samplesize = 0; double sigmathreshold = 0; int passcount = 0; int n = 0; int i = 0; int pass = 0; int s1 = 0; int s2 = 0; int i1 = 0; int i2 = 0; double r1 = 0; double r2 = 0; double[] x = new double[0]; double mean = 0; double means = 0; double stddev = 0; double stddevs = 0; double lambda = 0; bool seederrors = new bool(); bool urerrors = new bool(); double ursigmaerr = 0; bool uierrors = new bool(); double uisigmaerr = 0; bool normerrors = new bool(); double normsigmaerr = 0; bool experrors = new bool(); double expsigmaerr = 0; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); waserrors = false; sigmathreshold = 7; samplesize = 100000; passcount = 50; x = new double[samplesize - 1 + 1]; // // Test seed errors // seederrors = false; for (pass = 1; pass <= passcount; pass++) { s1 = 1 + AP.Math.RandomInteger(32000); s2 = 1 + AP.Math.RandomInteger(32000); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); i1 = hqrnd.hqrnduniformi(100, ref state); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); i2 = hqrnd.hqrnduniformi(100, ref state); seederrors = seederrors | i1 != i2; unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); r1 = hqrnd.hqrnduniformr(ref state); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); r2 = hqrnd.hqrnduniformr(ref state); seederrors = seederrors | (double)(r1) != (double)(r2); } // // Test HQRNDRandomize() and real uniform generator // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); urerrors = false; ursigmaerr = 0; for (i = 0; i <= samplesize - 1; i++) { x[i] = hqrnd.hqrnduniformr(ref state); } for (i = 0; i <= samplesize - 1; i++) { urerrors = urerrors | (double)(x[i]) <= (double)(0) | (double)(x[i]) >= (double)(1); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if ((double)(means) != (double)(0)) { ursigmaerr = Math.Max(ursigmaerr, Math.Abs((mean - 0.5) / means)); } else { urerrors = true; } if ((double)(stddevs) != (double)(0)) { ursigmaerr = Math.Max(ursigmaerr, Math.Abs((stddev - Math.Sqrt((double)(1) / (double)(12))) / stddevs)); } else { urerrors = true; } urerrors = urerrors | (double)(ursigmaerr) > (double)(sigmathreshold); // // Test HQRNDRandomize() and integer uniform // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); uierrors = false; uisigmaerr = 0; for (n = 2; n <= 10; n++) { for (i = 0; i <= samplesize - 1; i++) { x[i] = hqrnd.hqrnduniformi(n, ref state); } for (i = 0; i <= samplesize - 1; i++) { uierrors = uierrors | (double)(x[i]) < (double)(0) | (double)(x[i]) >= (double)(n); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if ((double)(means) != (double)(0)) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((mean - 0.5 * (n - 1)) / means)); } else { uierrors = true; } if ((double)(stddevs) != (double)(0)) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((stddev - Math.Sqrt((AP.Math.Sqr(n) - 1) / 12)) / stddevs)); } else { uierrors = true; } } uierrors = uierrors | (double)(uisigmaerr) > (double)(sigmathreshold); // // Special 'close-to-limit' test on uniformity of integers // (straightforward implementation like 'RND mod N' will return // non-uniform numbers for N=2/3*LIMIT) // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); uierrors = false; uisigmaerr = 0; n = (int)Math.Round(2.0 / 3.0 * 2147483563.0); for (i = 0; i <= samplesize - 1; i++) { x[i] = hqrnd.hqrnduniformi(n, ref state); } for (i = 0; i <= samplesize - 1; i++) { uierrors = uierrors | (double)(x[i]) < (double)(0) | (double)(x[i]) >= (double)(n); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if ((double)(means) != (double)(0)) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((mean - 0.5 * (n - 1)) / means)); } else { uierrors = true; } if ((double)(stddevs) != (double)(0)) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((stddev - Math.Sqrt((AP.Math.Sqr(n) - 1) / 12)) / stddevs)); } else { uierrors = true; } uierrors = uierrors | (double)(uisigmaerr) > (double)(sigmathreshold); // // Test normal // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); normerrors = false; normsigmaerr = 0; i = 0; while (i < samplesize) { hqrnd.hqrndnormal2(ref state, ref r1, ref r2); x[i] = r1; if (i + 1 < samplesize) { x[i + 1] = r2; } i = i + 2; } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if ((double)(means) != (double)(0)) { normsigmaerr = Math.Max(normsigmaerr, Math.Abs((mean - 0) / means)); } else { normerrors = true; } if ((double)(stddevs) != (double)(0)) { normsigmaerr = Math.Max(normsigmaerr, Math.Abs((stddev - 1) / stddevs)); } else { normerrors = true; } normerrors = normerrors | (double)(normsigmaerr) > (double)(sigmathreshold); // // Test exponential // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); experrors = false; expsigmaerr = 0; lambda = 2 + 5 * AP.Math.RandomReal(); for (i = 0; i <= samplesize - 1; i++) { x[i] = hqrnd.hqrndexponential(lambda, ref state); } for (i = 0; i <= samplesize - 1; i++) { uierrors = uierrors | (double)(x[i]) < (double)(0); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if ((double)(means) != (double)(0)) { expsigmaerr = Math.Max(expsigmaerr, Math.Abs((mean - 1.0 / lambda) / means)); } else { experrors = true; } if ((double)(stddevs) != (double)(0)) { expsigmaerr = Math.Max(expsigmaerr, Math.Abs((stddev - 1.0 / lambda) / stddevs)); } else { experrors = true; } experrors = experrors | (double)(expsigmaerr) > (double)(sigmathreshold); // // Final report // waserrors = seederrors | urerrors | uierrors | normerrors | experrors; if (!silent) { System.Console.Write("RNG TEST"); System.Console.WriteLine(); System.Console.Write("SEED TEST: "); if (!seederrors) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("UNIFORM CONTINUOUS: "); if (!urerrors) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("UNIFORM INTEGER: "); if (!uierrors) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("NORMAL: "); if (!normerrors) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("EXPONENTIAL: "); if (!experrors) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } if (waserrors) { System.Console.Write("TEST SUMMARY: FAILED"); System.Console.WriteLine(); } else { System.Console.Write("TEST SUMMARY: PASSED"); System.Console.WriteLine(); } System.Console.WriteLine(); System.Console.WriteLine(); } result = !waserrors; return(result); }
public static bool testhqrnd(bool silent) { bool result = new bool(); bool waserrors = new bool(); int samplesize = 0; double sigmathreshold = 0; int passcount = 0; int n = 0; int i = 0; int pass = 0; int s1 = 0; int s2 = 0; int i1 = 0; int i2 = 0; double r1 = 0; double r2 = 0; double[] x = new double[0]; double mean = 0; double means = 0; double stddev = 0; double stddevs = 0; double lambda = 0; bool seederrors = new bool(); bool urerrors = new bool(); double ursigmaerr = 0; bool uierrors = new bool(); double uisigmaerr = 0; bool normerrors = new bool(); double normsigmaerr = 0; bool experrors = new bool(); double expsigmaerr = 0; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); waserrors = false; sigmathreshold = 7; samplesize = 100000; passcount = 50; x = new double[samplesize-1+1]; // // Test seed errors // seederrors = false; for(pass=1; pass<=passcount; pass++) { s1 = 1+AP.Math.RandomInteger(32000); s2 = 1+AP.Math.RandomInteger(32000); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); i1 = hqrnd.hqrnduniformi(100, ref state); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); i2 = hqrnd.hqrnduniformi(100, ref state); seederrors = seederrors | i1!=i2; unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); r1 = hqrnd.hqrnduniformr(ref state); unsetstate(ref state); hqrnd.hqrndseed(s1, s2, ref state); r2 = hqrnd.hqrnduniformr(ref state); seederrors = seederrors | (double)(r1)!=(double)(r2); } // // Test HQRNDRandomize() and real uniform generator // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); urerrors = false; ursigmaerr = 0; for(i=0; i<=samplesize-1; i++) { x[i] = hqrnd.hqrnduniformr(ref state); } for(i=0; i<=samplesize-1; i++) { urerrors = urerrors | (double)(x[i])<=(double)(0) | (double)(x[i])>=(double)(1); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if( (double)(means)!=(double)(0) ) { ursigmaerr = Math.Max(ursigmaerr, Math.Abs((mean-0.5)/means)); } else { urerrors = true; } if( (double)(stddevs)!=(double)(0) ) { ursigmaerr = Math.Max(ursigmaerr, Math.Abs((stddev-Math.Sqrt((double)(1)/(double)(12)))/stddevs)); } else { urerrors = true; } urerrors = urerrors | (double)(ursigmaerr)>(double)(sigmathreshold); // // Test HQRNDRandomize() and integer uniform // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); uierrors = false; uisigmaerr = 0; for(n=2; n<=10; n++) { for(i=0; i<=samplesize-1; i++) { x[i] = hqrnd.hqrnduniformi(n, ref state); } for(i=0; i<=samplesize-1; i++) { uierrors = uierrors | (double)(x[i])<(double)(0) | (double)(x[i])>=(double)(n); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if( (double)(means)!=(double)(0) ) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((mean-0.5*(n-1))/means)); } else { uierrors = true; } if( (double)(stddevs)!=(double)(0) ) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((stddev-Math.Sqrt((AP.Math.Sqr(n)-1)/12))/stddevs)); } else { uierrors = true; } } uierrors = uierrors | (double)(uisigmaerr)>(double)(sigmathreshold); // // Special 'close-to-limit' test on uniformity of integers // (straightforward implementation like 'RND mod N' will return // non-uniform numbers for N=2/3*LIMIT) // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); uierrors = false; uisigmaerr = 0; n = (int)Math.Round(2.0/3.0*2147483563.0); for(i=0; i<=samplesize-1; i++) { x[i] = hqrnd.hqrnduniformi(n, ref state); } for(i=0; i<=samplesize-1; i++) { uierrors = uierrors | (double)(x[i])<(double)(0) | (double)(x[i])>=(double)(n); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if( (double)(means)!=(double)(0) ) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((mean-0.5*(n-1))/means)); } else { uierrors = true; } if( (double)(stddevs)!=(double)(0) ) { uisigmaerr = Math.Max(uisigmaerr, Math.Abs((stddev-Math.Sqrt((AP.Math.Sqr(n)-1)/12))/stddevs)); } else { uierrors = true; } uierrors = uierrors | (double)(uisigmaerr)>(double)(sigmathreshold); // // Test normal // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); normerrors = false; normsigmaerr = 0; i = 0; while( i<samplesize ) { hqrnd.hqrndnormal2(ref state, ref r1, ref r2); x[i] = r1; if( i+1<samplesize ) { x[i+1] = r2; } i = i+2; } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if( (double)(means)!=(double)(0) ) { normsigmaerr = Math.Max(normsigmaerr, Math.Abs((mean-0)/means)); } else { normerrors = true; } if( (double)(stddevs)!=(double)(0) ) { normsigmaerr = Math.Max(normsigmaerr, Math.Abs((stddev-1)/stddevs)); } else { normerrors = true; } normerrors = normerrors | (double)(normsigmaerr)>(double)(sigmathreshold); // // Test exponential // unsetstate(ref state); hqrnd.hqrndrandomize(ref state); experrors = false; expsigmaerr = 0; lambda = 2+5*AP.Math.RandomReal(); for(i=0; i<=samplesize-1; i++) { x[i] = hqrnd.hqrndexponential(lambda, ref state); } for(i=0; i<=samplesize-1; i++) { uierrors = uierrors | (double)(x[i])<(double)(0); } calculatemv(ref x, samplesize, ref mean, ref means, ref stddev, ref stddevs); if( (double)(means)!=(double)(0) ) { expsigmaerr = Math.Max(expsigmaerr, Math.Abs((mean-1.0/lambda)/means)); } else { experrors = true; } if( (double)(stddevs)!=(double)(0) ) { expsigmaerr = Math.Max(expsigmaerr, Math.Abs((stddev-1.0/lambda)/stddevs)); } else { experrors = true; } experrors = experrors | (double)(expsigmaerr)>(double)(sigmathreshold); // // Final report // waserrors = seederrors | urerrors | uierrors | normerrors | experrors; if( !silent ) { System.Console.Write("RNG TEST"); System.Console.WriteLine(); System.Console.Write("SEED TEST: "); if( !seederrors ) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("UNIFORM CONTINUOUS: "); if( !urerrors ) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("UNIFORM INTEGER: "); if( !uierrors ) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("NORMAL: "); if( !normerrors ) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } System.Console.Write("EXPONENTIAL: "); if( !experrors ) { System.Console.Write("OK"); System.Console.WriteLine(); } else { System.Console.Write("FAILED"); System.Console.WriteLine(); } if( waserrors ) { System.Console.Write("TEST SUMMARY: FAILED"); System.Console.WriteLine(); } else { System.Console.Write("TEST SUMMARY: PASSED"); System.Console.WriteLine(); } System.Console.WriteLine(); System.Console.WriteLine(); } result = !waserrors; return result; }
/************************************************************************* Hermitian multiplication of NxN matrix by random Haar distributed complex orthogonal matrix INPUT PARAMETERS: A - matrix, array[0..N-1, 0..N-1] N - matrix size OUTPUT PARAMETERS: A - Q^H*A*Q, where Q is random NxN orthogonal matrix -- ALGLIB routine -- 04.12.2009 Bochkanov Sergey *************************************************************************/ public static void hmatrixrndmultiply(ref AP.Complex[,] a, int n) { AP.Complex tau = 0; AP.Complex lambda = 0; int s = 0; int i = 0; AP.Complex[] w = new AP.Complex[0]; AP.Complex[] v = new AP.Complex[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; // // General case. // w = new AP.Complex[n-1+1]; v = new AP.Complex[n+1]; hqrnd.hqrndrandomize(ref state); for(s=2; s<=n; s++) { // // Prepare random normal v // do { for(i=1; i<=s; i++) { hqrnd.hqrndnormal2(ref state, ref tau.x, ref tau.y); v[i] = tau; } lambda = 0.0; for(i_=1; i_<=s;i_++) { lambda += v[i_]*AP.Math.Conj(v[i_]); } } while( lambda==0 ); // // Prepare and apply reflection // creflections.complexgeneratereflection(ref v, s, ref tau); v[1] = 1; creflections.complexapplyreflectionfromtheright(ref a, tau, ref v, 0, n-1, n-s, n-1, ref w); creflections.complexapplyreflectionfromtheleft(ref a, AP.Math.Conj(tau), ref v, n-s, n-1, 0, n-1, ref w); } // // Second pass. // for(i=0; i<=n-1; i++) { hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for(i_=0; i_<=n-1;i_++) { a[i_,i] = tau*a[i_,i]; } tau = AP.Math.Conj(tau); for(i_=0; i_<=n-1;i_++) { a[i,i_] = tau*a[i,i_]; } } }
/************************************************************************* Symmetric multiplication of NxN matrix by random Haar distributed orthogonal matrix INPUT PARAMETERS: A - matrix, array[0..N-1, 0..N-1] N - matrix size OUTPUT PARAMETERS: A - Q'*A*Q, where Q is random NxN orthogonal matrix -- ALGLIB routine -- 04.12.2009 Bochkanov Sergey *************************************************************************/ public static void smatrixrndmultiply(ref double[,] a, int n) { double tau = 0; double lambda = 0; int s = 0; int i = 0; double u1 = 0; double u2 = 0; double[] w = new double[0]; double[] v = new double[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; // // General case. // w = new double[n-1+1]; v = new double[n+1]; hqrnd.hqrndrandomize(ref state); for(s=2; s<=n; s++) { // // Prepare random normal v // do { i = 1; while( i<=s ) { hqrnd.hqrndnormal2(ref state, ref u1, ref u2); v[i] = u1; if( i+1<=s ) { v[i+1] = u2; } i = i+2; } lambda = 0.0; for(i_=1; i_<=s;i_++) { lambda += v[i_]*v[i_]; } } while( (double)(lambda)==(double)(0) ); // // Prepare and apply reflection // reflections.generatereflection(ref v, s, ref tau); v[1] = 1; reflections.applyreflectionfromtheright(ref a, tau, ref v, 0, n-1, n-s, n-1, ref w); reflections.applyreflectionfromtheleft(ref a, tau, ref v, n-s, n-1, 0, n-1, ref w); } // // Second pass. // for(i=0; i<=n-1; i++) { tau = 2*AP.Math.RandomInteger(2)-1; for(i_=0; i_<=n-1;i_++) { a[i_,i] = tau*a[i_,i]; } for(i_=0; i_<=n-1;i_++) { a[i,i_] = tau*a[i,i_]; } } }
/************************************************************************* Multiplication of MxN complex matrix by MxM random Haar distributed complex orthogonal matrix INPUT PARAMETERS: A - matrix, array[0..M-1, 0..N-1] M, N- matrix size OUTPUT PARAMETERS: A - Q*A, where Q is random MxM orthogonal matrix -- ALGLIB routine -- 04.12.2009 Bochkanov Sergey *************************************************************************/ public static void cmatrixrndorthogonalfromtheleft(ref AP.Complex[,] a, int m, int n) { AP.Complex tau = 0; AP.Complex lambda = 0; int s = 0; int i = 0; int j = 0; AP.Complex[] w = new AP.Complex[0]; AP.Complex[] v = new AP.Complex[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; System.Diagnostics.Debug.Assert(n>=1 & m>=1, "CMatrixRndOrthogonalFromTheRight: N<1 or M<1!"); if( m==1 ) { // // special case // hqrnd.hqrndrandomize(ref state); hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for(j=0; j<=n-1; j++) { a[0,j] = a[0,j]*tau; } return; } // // General case. // First pass. // w = new AP.Complex[n-1+1]; v = new AP.Complex[m+1]; hqrnd.hqrndrandomize(ref state); for(s=2; s<=m; s++) { // // Prepare random normal v // do { for(i=1; i<=s; i++) { hqrnd.hqrndnormal2(ref state, ref tau.x, ref tau.y); v[i] = tau; } lambda = 0.0; for(i_=1; i_<=s;i_++) { lambda += v[i_]*AP.Math.Conj(v[i_]); } } while( lambda==0 ); // // Prepare and apply reflection // creflections.complexgeneratereflection(ref v, s, ref tau); v[1] = 1; creflections.complexapplyreflectionfromtheleft(ref a, tau, ref v, m-s, m-1, 0, n-1, ref w); } // // Second pass. // for(i=0; i<=m-1; i++) { hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for(i_=0; i_<=n-1;i_++) { a[i,i_] = tau*a[i,i_]; } } }
/************************************************************************* Multiplication of MxN matrix by MxM random Haar distributed orthogonal matrix INPUT PARAMETERS: A - matrix, array[0..M-1, 0..N-1] M, N- matrix size OUTPUT PARAMETERS: A - Q*A, where Q is random MxM orthogonal matrix -- ALGLIB routine -- 04.12.2009 Bochkanov Sergey *************************************************************************/ public static void rmatrixrndorthogonalfromtheleft(ref double[,] a, int m, int n) { double tau = 0; double lambda = 0; int s = 0; int i = 0; int j = 0; double u1 = 0; double u2 = 0; double[] w = new double[0]; double[] v = new double[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; System.Diagnostics.Debug.Assert(n>=1 & m>=1, "RMatrixRndOrthogonalFromTheRight: N<1 or M<1!"); if( m==1 ) { // // special case // tau = 2*AP.Math.RandomInteger(2)-1; for(j=0; j<=n-1; j++) { a[0,j] = a[0,j]*tau; } return; } // // General case. // First pass. // w = new double[n-1+1]; v = new double[m+1]; hqrnd.hqrndrandomize(ref state); for(s=2; s<=m; s++) { // // Prepare random normal v // do { i = 1; while( i<=s ) { hqrnd.hqrndnormal2(ref state, ref u1, ref u2); v[i] = u1; if( i+1<=s ) { v[i+1] = u2; } i = i+2; } lambda = 0.0; for(i_=1; i_<=s;i_++) { lambda += v[i_]*v[i_]; } } while( (double)(lambda)==(double)(0) ); // // Prepare and apply reflection // reflections.generatereflection(ref v, s, ref tau); v[1] = 1; reflections.applyreflectionfromtheleft(ref a, tau, ref v, m-s, m-1, 0, n-1, ref w); } // // Second pass. // for(i=0; i<=m-1; i++) { tau = 2*AP.Math.RandomInteger(2)-1; for(i_=0; i_<=n-1;i_++) { a[i,i_] = tau*a[i,i_]; } } }
/************************************************************************* Generation of random NxN complex matrix with given condition number C and norm2(A)=1 INPUT PARAMETERS: N - matrix size C - condition number (in 2-norm) OUTPUT PARAMETERS: A - random matrix with norm2(A)=1 and cond(A)=C -- ALGLIB routine -- 04.12.2009 Bochkanov Sergey *************************************************************************/ public static void cmatrixrndcond(int n, double c, ref AP.Complex[,] a) { int i = 0; int j = 0; double l1 = 0; double l2 = 0; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); AP.Complex v = 0; System.Diagnostics.Debug.Assert(n>=1 & (double)(c)>=(double)(1), "CMatrixRndCond: N<1 or C<1!"); a = new AP.Complex[n-1+1, n-1+1]; if( n==1 ) { // // special case // hqrnd.hqrndrandomize(ref state); hqrnd.hqrndunit2(ref state, ref v.x, ref v.y); a[0,0] = v; return; } l1 = 0; l2 = Math.Log(1/c); for(i=0; i<=n-1; i++) { for(j=0; j<=n-1; j++) { a[i,j] = 0; } } a[0,0] = Math.Exp(l1); for(i=1; i<=n-2; i++) { a[i,i] = Math.Exp(AP.Math.RandomReal()*(l2-l1)+l1); } a[n-1,n-1] = Math.Exp(l2); cmatrixrndorthogonalfromtheleft(ref a, n, n); cmatrixrndorthogonalfromtheright(ref a, n, n); }
/************************************************************************* * Hermitian multiplication of NxN matrix by random Haar distributed * complex orthogonal matrix * * INPUT PARAMETERS: * A - matrix, array[0..N-1, 0..N-1] * N - matrix size * * OUTPUT PARAMETERS: * A - Q^H*A*Q, where Q is random NxN orthogonal matrix * * -- ALGLIB routine -- * 04.12.2009 * Bochkanov Sergey *************************************************************************/ public static void hmatrixrndmultiply(ref AP.Complex[,] a, int n) { AP.Complex tau = 0; AP.Complex lambda = 0; int s = 0; int i = 0; AP.Complex[] w = new AP.Complex[0]; AP.Complex[] v = new AP.Complex[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; // // General case. // w = new AP.Complex[n - 1 + 1]; v = new AP.Complex[n + 1]; hqrnd.hqrndrandomize(ref state); for (s = 2; s <= n; s++) { // // Prepare random normal v // do { for (i = 1; i <= s; i++) { hqrnd.hqrndnormal2(ref state, ref tau.x, ref tau.y); v[i] = tau; } lambda = 0.0; for (i_ = 1; i_ <= s; i_++) { lambda += v[i_] * AP.Math.Conj(v[i_]); } }while(lambda == 0); // // Prepare and apply reflection // creflections.complexgeneratereflection(ref v, s, ref tau); v[1] = 1; creflections.complexapplyreflectionfromtheright(ref a, tau, ref v, 0, n - 1, n - s, n - 1, ref w); creflections.complexapplyreflectionfromtheleft(ref a, AP.Math.Conj(tau), ref v, n - s, n - 1, 0, n - 1, ref w); } // // Second pass. // for (i = 0; i <= n - 1; i++) { hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for (i_ = 0; i_ <= n - 1; i_++) { a[i_, i] = tau * a[i_, i]; } tau = AP.Math.Conj(tau); for (i_ = 0; i_ <= n - 1; i_++) { a[i, i_] = tau * a[i, i_]; } } }
/************************************************************************* * Symmetric multiplication of NxN matrix by random Haar distributed * orthogonal matrix * * INPUT PARAMETERS: * A - matrix, array[0..N-1, 0..N-1] * N - matrix size * * OUTPUT PARAMETERS: * A - Q'*A*Q, where Q is random NxN orthogonal matrix * * -- ALGLIB routine -- * 04.12.2009 * Bochkanov Sergey *************************************************************************/ public static void smatrixrndmultiply(ref double[,] a, int n) { double tau = 0; double lambda = 0; int s = 0; int i = 0; double u1 = 0; double u2 = 0; double[] w = new double[0]; double[] v = new double[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; // // General case. // w = new double[n - 1 + 1]; v = new double[n + 1]; hqrnd.hqrndrandomize(ref state); for (s = 2; s <= n; s++) { // // Prepare random normal v // do { i = 1; while (i <= s) { hqrnd.hqrndnormal2(ref state, ref u1, ref u2); v[i] = u1; if (i + 1 <= s) { v[i + 1] = u2; } i = i + 2; } lambda = 0.0; for (i_ = 1; i_ <= s; i_++) { lambda += v[i_] * v[i_]; } }while((double)(lambda) == (double)(0)); // // Prepare and apply reflection // reflections.generatereflection(ref v, s, ref tau); v[1] = 1; reflections.applyreflectionfromtheright(ref a, tau, ref v, 0, n - 1, n - s, n - 1, ref w); reflections.applyreflectionfromtheleft(ref a, tau, ref v, n - s, n - 1, 0, n - 1, ref w); } // // Second pass. // for (i = 0; i <= n - 1; i++) { tau = 2 * AP.Math.RandomInteger(2) - 1; for (i_ = 0; i_ <= n - 1; i_++) { a[i_, i] = tau * a[i_, i]; } for (i_ = 0; i_ <= n - 1; i_++) { a[i, i_] = tau * a[i, i_]; } } }
/************************************************************************* * Multiplication of MxN complex matrix by MxM random Haar distributed * complex orthogonal matrix * * INPUT PARAMETERS: * A - matrix, array[0..M-1, 0..N-1] * M, N- matrix size * * OUTPUT PARAMETERS: * A - Q*A, where Q is random MxM orthogonal matrix * * -- ALGLIB routine -- * 04.12.2009 * Bochkanov Sergey *************************************************************************/ public static void cmatrixrndorthogonalfromtheleft(ref AP.Complex[,] a, int m, int n) { AP.Complex tau = 0; AP.Complex lambda = 0; int s = 0; int i = 0; int j = 0; AP.Complex[] w = new AP.Complex[0]; AP.Complex[] v = new AP.Complex[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; System.Diagnostics.Debug.Assert(n >= 1 & m >= 1, "CMatrixRndOrthogonalFromTheRight: N<1 or M<1!"); if (m == 1) { // // special case // hqrnd.hqrndrandomize(ref state); hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for (j = 0; j <= n - 1; j++) { a[0, j] = a[0, j] * tau; } return; } // // General case. // First pass. // w = new AP.Complex[n - 1 + 1]; v = new AP.Complex[m + 1]; hqrnd.hqrndrandomize(ref state); for (s = 2; s <= m; s++) { // // Prepare random normal v // do { for (i = 1; i <= s; i++) { hqrnd.hqrndnormal2(ref state, ref tau.x, ref tau.y); v[i] = tau; } lambda = 0.0; for (i_ = 1; i_ <= s; i_++) { lambda += v[i_] * AP.Math.Conj(v[i_]); } }while(lambda == 0); // // Prepare and apply reflection // creflections.complexgeneratereflection(ref v, s, ref tau); v[1] = 1; creflections.complexapplyreflectionfromtheleft(ref a, tau, ref v, m - s, m - 1, 0, n - 1, ref w); } // // Second pass. // for (i = 0; i <= m - 1; i++) { hqrnd.hqrndunit2(ref state, ref tau.x, ref tau.y); for (i_ = 0; i_ <= n - 1; i_++) { a[i, i_] = tau * a[i, i_]; } } }
/************************************************************************* * Multiplication of MxN matrix by MxM random Haar distributed orthogonal matrix * * INPUT PARAMETERS: * A - matrix, array[0..M-1, 0..N-1] * M, N- matrix size * * OUTPUT PARAMETERS: * A - Q*A, where Q is random MxM orthogonal matrix * * -- ALGLIB routine -- * 04.12.2009 * Bochkanov Sergey *************************************************************************/ public static void rmatrixrndorthogonalfromtheleft(ref double[,] a, int m, int n) { double tau = 0; double lambda = 0; int s = 0; int i = 0; int j = 0; double u1 = 0; double u2 = 0; double[] w = new double[0]; double[] v = new double[0]; hqrnd.hqrndstate state = new hqrnd.hqrndstate(); int i_ = 0; System.Diagnostics.Debug.Assert(n >= 1 & m >= 1, "RMatrixRndOrthogonalFromTheRight: N<1 or M<1!"); if (m == 1) { // // special case // tau = 2 * AP.Math.RandomInteger(2) - 1; for (j = 0; j <= n - 1; j++) { a[0, j] = a[0, j] * tau; } return; } // // General case. // First pass. // w = new double[n - 1 + 1]; v = new double[m + 1]; hqrnd.hqrndrandomize(ref state); for (s = 2; s <= m; s++) { // // Prepare random normal v // do { i = 1; while (i <= s) { hqrnd.hqrndnormal2(ref state, ref u1, ref u2); v[i] = u1; if (i + 1 <= s) { v[i + 1] = u2; } i = i + 2; } lambda = 0.0; for (i_ = 1; i_ <= s; i_++) { lambda += v[i_] * v[i_]; } }while((double)(lambda) == (double)(0)); // // Prepare and apply reflection // reflections.generatereflection(ref v, s, ref tau); v[1] = 1; reflections.applyreflectionfromtheleft(ref a, tau, ref v, m - s, m - 1, 0, n - 1, ref w); } // // Second pass. // for (i = 0; i <= m - 1; i++) { tau = 2 * AP.Math.RandomInteger(2) - 1; for (i_ = 0; i_ <= n - 1; i_++) { a[i, i_] = tau * a[i, i_]; } } }