// // End SetInitialPowerVector(double[][] TobeSet) // Calculate Matrix Global Vector product storing as a distributed vector public static void FindTraceandNorm(double[][,] MatrixDiagonals, double[][] GlobalxVector, ref double Trace, ref double Norm) { Trace = 0.0; Norm = 0.0; var FindTrace = new GlobalReductions.FindDoubleSum(SALSAUtility.ThreadCount); var FindNorm = new GlobalReductions.FindDoubleMax(SALSAUtility.ThreadCount); Parallel.For(0, SALSAUtility.ParallelOptions.MaxDegreeOfParallelism, SALSAUtility.ParallelOptions, (ThreadNo) => { // Start Code to calculate Trace and Norm double LocalTrace = 0.0; double LocalNorm = 0.0; double WeightFunction1, WeightFunction2; int indexlen = SALSAUtility.PointsperThread[ThreadNo]; int beginpoint = SALSAUtility.StartPointperThread[ThreadNo] - SALSAUtility.PointStart_Process; for (int LongIndex1 = beginpoint; LongIndex1 < indexlen + beginpoint; LongIndex1++) { int GlobalIndex1 = LongIndex1 + SALSAUtility.PointStart_Process; for (int LocalVectorIndex1 = 0; LocalVectorIndex1 < Hotsun.ParameterVectorDimension; LocalVectorIndex1++) { if (Hotsun.FixedParameter[GlobalIndex1][LocalVectorIndex1]) { continue; } double RowNorm = 0.0; for (int GlobalIndex2 = 0; GlobalIndex2 < SALSAUtility.PointCount_Global; GlobalIndex2++) { if ( !ManxcatMDS.SetChisqWeights(GlobalIndex1, GlobalIndex2, out WeightFunction1, out WeightFunction2)) { continue; } double DistanceFudge1 = 1.0; double DistanceFudge2 = 1.0; double SquaredDistance = 0.0; double funcl = 0.0; if (GlobalIndex1 != GlobalIndex2) { for (int LocalVectorIndex2 = 0; LocalVectorIndex2 < Hotsun.ParameterVectorDimension; LocalVectorIndex2++) { double tmp1 = GlobalxVector[GlobalIndex1][LocalVectorIndex2] - GlobalxVector[GlobalIndex2][LocalVectorIndex2]; SquaredDistance += tmp1 * tmp1; } double ActualDistance = SquaredDistance; if (UserRoutines.DistanceFormula == 1) { SquaredDistance += UserRoutines.SQUAREMinimumDistance; ActualDistance = Math.Sqrt(SquaredDistance); DistanceFudge1 = 0.5 / ActualDistance; DistanceFudge2 = DistanceFudge1 / SquaredDistance; } funcl = WeightFunction1 - WeightFunction2 * ActualDistance; } for (int LocalVectorIndex2 = 0; LocalVectorIndex2 < Hotsun.ParameterVectorDimension; LocalVectorIndex2++) { if (Hotsun.FixedParameter[GlobalIndex2][LocalVectorIndex2]) { continue; } double MatrixElement; if (GlobalIndex1 == GlobalIndex2) { // Diagonal Term MatrixElement = MatrixDiagonals[LongIndex1][ LocalVectorIndex1, LocalVectorIndex2]; if (Hotsun.UseDiagonalScaling) { MatrixElement *= Hotsun.sqdginv[GlobalIndex1][LocalVectorIndex1] * Hotsun.sqdginv[GlobalIndex1][LocalVectorIndex2]; } if (Hotsun.AddMarquardtQDynamically && (LocalVectorIndex1 == LocalVectorIndex2)) { MatrixElement += Hotsun.Q; } if (LocalVectorIndex1 == LocalVectorIndex2) { LocalTrace += Math.Abs(MatrixElement); } } else { // Off Diagonal Term double correction = 0.0; double VectorCrossProduct = (GlobalxVector[GlobalIndex1][LocalVectorIndex1] - GlobalxVector[GlobalIndex2][LocalVectorIndex1]) * (GlobalxVector[GlobalIndex1][LocalVectorIndex2] - GlobalxVector[GlobalIndex2][LocalVectorIndex2]); MatrixElement = -8.0 * VectorCrossProduct * DistanceFudge1 * DistanceFudge1 * WeightFunction2 * WeightFunction2; if (Hotsun.FullSecondDerivative) { if ((UserRoutines.DistanceFormula == 2) && (LocalVectorIndex1 == LocalVectorIndex2)) { correction = 4.0 * funcl * WeightFunction2; } if ((UserRoutines.DistanceFormula == 1) && (LocalVectorIndex1 == LocalVectorIndex2)) { correction = 4.0 * funcl * WeightFunction2 * DistanceFudge1; } if (UserRoutines.DistanceFormula == 1) { correction += -4.0 * funcl * VectorCrossProduct * WeightFunction2 * DistanceFudge2; } MatrixElement += correction; } if (Hotsun.UseDiagonalScaling) { MatrixElement *= Hotsun.sqdginv[GlobalIndex1][LocalVectorIndex1] * Hotsun.sqdginv[GlobalIndex2][LocalVectorIndex2]; } } RowNorm += Math.Abs(MatrixElement); } // End Loop over LocalVectorIndex2 } // End loop over GlobalIndex2 if (LocalNorm < RowNorm) { LocalNorm = RowNorm; } } // End Loop over LocalVectorIndex1 } // // End loop over LongIndex1 FindTrace.addapoint(ThreadNo, LocalTrace); FindNorm.addapoint(ThreadNo, LocalNorm); }); // End loop over Point dependent quantities FindTrace.sumoverthreadsandmpi(); FindNorm.sumoverthreadsandmpi(); Trace = FindTrace.Total; Norm = FindNorm.TotalMax; return; }
// End UpdateManxcatMDS_Option12() public static void UpdateManxcatMDS_Option12_Functions(bool statistics) { // Setup weight: Only used in statistics var weights = new double[SALSAUtility.PointCount_Global]; ManxcatMDS.WeightingOption = ManxcatCentral.Configuration.WeightingOption; ManxcatMDS.SetupWeightings(weights); string TypicalMDSFileName = ""; // Setup MDS: Only used in statistics var MDSPoints = new double[SALSAUtility.PointCount_Global, Hotsun.ParameterVectorDimension]; if (statistics) { // Set up statistics by reading MDS file TypicalMDSFileName = ManxcatCentral.ResultDirectoryName + "\\SIMPLE" + ManxcatCentral.Configuration.ReducedVectorOutputFileName; if (!File.Exists(TypicalMDSFileName)) { Exception e = SALSAUtility.SALSAError(" File " + TypicalMDSFileName + " Does Not Exist"); throw (e); } int NumberMDSPoints = 0; var InitialMDSString = new string[SALSAUtility.PointCount_Global]; var ColorValue = new int[SALSAUtility.PointCount_Global]; if (ReadMDSCluster_File(TypicalMDSFileName, InitialMDSString, ColorValue, ref NumberMDSPoints)) { SALSAUtility.SALSAPrint(0, "MDS File " + TypicalMDSFileName + " Read Successfully"); } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { // Extract MDS POints string inputLineStr = InitialMDSString[PointIndex].Trim(); string[] split = inputLineStr.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length < Hotsun.ParameterVectorDimension) { Exception e = SALSAUtility.SALSAError(" Point " + PointIndex.ToString() + " has wrong number of Entries " + inputLineStr + " Entries" + split.Length); throw (e); } for (int VectorIndex = 0; VectorIndex < Hotsun.ParameterVectorDimension; VectorIndex++) { MDSPoints[PointIndex, VectorIndex] = Convert.ToDouble(split[VectorIndex].Trim()); } } } // End set up of Statistics // Set Mapping var ClusterLabel = new int[SALSAUtility.PointCount_Global]; int NumDivisions = 10; int BasicSize = SALSAUtility.PointCount_Global / NumDivisions; int LeftOver = SALSAUtility.PointCount_Global - BasicSize * NumDivisions; int DivisionCount = 0; int Limit = 0; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { if (PointIndex >= Limit) { ++DivisionCount; Limit += BasicSize; if (LeftOver >= DivisionCount) { ++Limit; } } ClusterLabel[PointIndex] = DivisionCount; } String FunctionFileName = ManxcatCentral.Configuration.ConversionInformation; if (!FunctionFileName.Contains(":")) { FunctionFileName = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + FunctionFileName; } var functionkeys = new double[SALSAUtility.PointCount_Global]; var functionlabels = new int[SALSAUtility.PointCount_Global]; int pickout = -1; while (true) { ++pickout; bool endofdata = true; int NumberofLines = 0; double sumabs = 0.0; double totalweight = 0.0; double meangamma = 0.0; double vargamma = 0.0; var meanxyz = new double[Hotsun.ParameterVectorDimension]; var varxyz = new double[Hotsun.ParameterVectorDimension, Hotsun.ParameterVectorDimension]; var correlxyzgamma = new double[Hotsun.ParameterVectorDimension]; if (statistics) { for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] = 0.0; for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] = 0.0; } correlxyzgamma[VectorIndex1] = 0.0; } } try { // Check if file exists if (!File.Exists(FunctionFileName)) { Exception e = SALSAUtility.SALSAError("File " + FunctionFileName + " does not exists."); throw (e); } // Create a new stream to read from a file using (StreamReader sr = File.OpenText(FunctionFileName)) { // Read contents of a file, line by line, into a string String inputLineStr; while ((inputLineStr = sr.ReadLine()) != null) { inputLineStr = inputLineStr.Trim(); string[] split = inputLineStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length <= pickout) { break; } endofdata = false; string usethis = split[pickout].Trim(); if (usethis.Length < 1) { continue; //replace empty line } double gamma = Convert.ToDouble(usethis); functionkeys[NumberofLines] = gamma; functionlabels[NumberofLines] = NumberofLines; sumabs += Math.Abs(gamma); if (statistics) { double wgt = weights[NumberofLines]; meangamma += gamma * wgt; vargamma += wgt * gamma * gamma; totalweight += wgt; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] += MDSPoints[NumberofLines, VectorIndex1] * wgt; correlxyzgamma[VectorIndex1] += MDSPoints[NumberofLines, VectorIndex1] * gamma * wgt; for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] += MDSPoints[NumberofLines, VectorIndex1] * MDSPoints[NumberofLines, VectorIndex2] * wgt; } } } ++NumberofLines; } sr.Close(); } } catch (Exception e) { SALSAUtility.SALSAError("Failed to read data from " + FunctionFileName + " " + e); throw (e); } if (endofdata) { break; } if (NumberofLines != SALSAUtility.PointCount_Global) { SALSAUtility.SALSAError("Incorrect Function count read " + NumberofLines + " Expected " + SALSAUtility.PointCount_Global); } // Set Statistics if (statistics) { var alpha = new double[Hotsun.ParameterVectorDimension]; double alphanorm = 0.0; meangamma = meangamma / totalweight; vargamma = (vargamma / totalweight) - meangamma * meangamma; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] = meanxyz[VectorIndex1] / totalweight; } for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] = (varxyz[VectorIndex1, VectorIndex2] / totalweight) - meanxyz[VectorIndex1] * meanxyz[VectorIndex2]; } correlxyzgamma[VectorIndex1] = correlxyzgamma[VectorIndex1] / totalweight - meangamma * meanxyz[VectorIndex1]; alpha[VectorIndex1] = correlxyzgamma[VectorIndex1] / varxyz[VectorIndex1, VectorIndex1]; alphanorm += alpha[VectorIndex1] * alpha[VectorIndex1]; } // invert Matrix to find best alpha var ConventionalFirst = new double[Hotsun.ParameterVectorDimension, 1]; var ConventionalAnswer = new double[Hotsun.ParameterVectorDimension][]; var ConventionalMatrix = new double[Hotsun.ParameterVectorDimension, Hotsun.ParameterVectorDimension]; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { ConventionalAnswer[VectorIndex1] = new double[1]; ConventionalFirst[VectorIndex1, 0] = correlxyzgamma[VectorIndex1] / Math.Sqrt(varxyz[VectorIndex1, VectorIndex1]); for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { ConventionalMatrix[VectorIndex1, VectorIndex2] = varxyz[VectorIndex1, VectorIndex2] / Math.Sqrt( varxyz[VectorIndex1, VectorIndex1] * varxyz[VectorIndex2, VectorIndex2]); } } LMatrix cMatrix = LMatrix.Create(ConventionalMatrix); LMatrix RightHandSide = LMatrix.Create(ConventionalFirst); LMatrix MatrixAnswer = cMatrix.Solve(RightHandSide); ConventionalAnswer = MatrixAnswer; alphanorm = 0.0; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { alpha[VectorIndex1] = ConventionalAnswer[VectorIndex1][0] / Math.Sqrt(varxyz[VectorIndex1, VectorIndex1]); alphanorm += alpha[VectorIndex1] * alpha[VectorIndex1]; } double Fullcorrelation = 0.0; double varalphaxyz = 0.0; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { alpha[VectorIndex1] = alpha[VectorIndex1] / Math.Sqrt(alphanorm); Fullcorrelation += alpha[VectorIndex1] * correlxyzgamma[VectorIndex1]; } for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varalphaxyz += alpha[VectorIndex1] * alpha[VectorIndex2] * varxyz[VectorIndex1, VectorIndex2]; } } Console.WriteLine(varalphaxyz + " " + Fullcorrelation + " " + vargamma); Fullcorrelation = Fullcorrelation / (Math.Sqrt(vargamma * varalphaxyz)); SALSAUtility.SALSAPrint(0, "Column " + pickout.ToString() + " File " + FunctionFileName + " MDS File " + TypicalMDSFileName + " Total Weight " + totalweight.ToString() + " Correlation " + Fullcorrelation.ToString("F4") + " Direction " + alpha[0].ToString("F4") + " " + alpha[1].ToString("F4") + " " + alpha[2].ToString("F4")); } // Set Divisions Array.Sort(functionkeys, functionlabels); var PointColors = new int[SALSAUtility.PointCount_Global]; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int UnsortedPosition = functionlabels[PointIndex]; PointColors[UnsortedPosition] = ClusterLabel[PointIndex]; } string OutputFileExtension = ManxcatCentral.Configuration.ConversionInformation; OutputFileExtension = OutputFileExtension.Replace(".dat", ""); OutputFileExtension = OutputFileExtension.Replace(".txt", ""); OutputFileExtension = OutputFileExtension + "-" + pickout.ToString() + ".txt"; string labelFileDirectory = ManxcatCentral.Configuration.ClusterDirectory; // Directory for Colors attached to Points string OutputFileName = labelFileDirectory + "\\" + OutputFileExtension; WritePointCluster(OutputFileName, PointColors, SALSAUtility.PointCount_Global); double test = 0.1 * sumabs / NumberofLines; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int UnsortedPosition = functionlabels[PointIndex]; if (Math.Abs(functionkeys[UnsortedPosition]) < test) { PointColors[UnsortedPosition] = 0; } } OutputFileName = OutputFileName.Replace(".txt", "NZ.txt"); WritePointCluster(OutputFileName, PointColors, SALSAUtility.PointCount_Global); } // End while over pickout (columns in data) return; }