public void SaveAsPDF(string filename = "", bool absolutePath = true) { if (filename == "") { return; } int pointsCount = 101; string matlabScript = "clear;" + Environment.NewLine; matlabScript += "fig = figure;" + Environment.NewLine; matlabScript += "set(fig,'units','normalized','outerposition',[0 0 1 1]);" + Environment.NewLine; matlabScript += "hold on;" + Environment.NewLine; double xSpaceDiff = (xSpaceMax - xSpaceMin) / (double)(pointsCount - 1); if (theRepresentingFunctions.Count > 0) { DenseVector xSpaceML = DenseVector.Create(pointsCount, new Func <int, double>(i => { return(xSpaceMin + ((double)i / (double)pointsCount) * (xSpaceMax - xSpaceMin)); })); foreach (Tuple <int, double> tuple in xSpaceML.EnumerateIndexed()) { matlabScript += "xSpace(" + (tuple.Item1 + 1).ToString() + ") = " + tuple.Item2.ToString("e").Replace(",", ".") + ";" + Environment.NewLine; } int funcCounter = 0; for (int funcIndex = 0; funcIndex < theRepresentingFunctions.Count; funcIndex++) { Func <DenseVector, double, double> function = theRepresentingFunctions[funcIndex]; DenseVector parametersVector = parameters[funcIndex]; DenseVector dvCurrFuncValues = DenseVector.Create(pointsCount, new Func <int, double>(i => function(parametersVector, xSpaceML[i]))); foreach (Tuple <int, double> tuple in dvCurrFuncValues.EnumerateIndexed()) { matlabScript += "func" + funcIndex.ToString() + "(" + (tuple.Item1 + 1).ToString() + ") = " + tuple.Item2.ToString("e").Replace(",", ".") + ";" + Environment.NewLine; } } for (int i = 0; i < theRepresentingFunctions.Count; i++) { Bgr CurrColor = lineColors[i]; string colR = (CurrColor.Red / 255.0d).ToString("e").Replace(",", "."); string colG = (CurrColor.Green / 255.0d).ToString("e").Replace(",", "."); string colB = (CurrColor.Blue / 255.0d).ToString("e").Replace(",", "."); matlabScript += "plot(xSpace, func" + i + ", 'Color', [" + colR + " " + colG + " " + colB + "], 'LineWidth', 2.0);" + Environment.NewLine; } } if ((dvScatterXSpace != null) && (dvScatterFuncValues != null)) { foreach (Tuple <int, double> tuple in dvScatterXSpace.EnumerateIndexed()) { matlabScript += "scatterXSpace(" + (tuple.Item1 + 1).ToString() + ") = " + tuple.Item2.ToString("e").Replace(",", ".") + ";" + Environment.NewLine; } foreach (Tuple <int, double> tuple in dvScatterFuncValues.EnumerateIndexed()) { matlabScript += "scatterFuncVals(" + (tuple.Item1 + 1).ToString() + ") = " + tuple.Item2.ToString("e").Replace(",", ".") + ";" + Environment.NewLine; } matlabScript += "scatter(scatterXSpace, scatterFuncVals, 'bo', 'LineWidth', 2);" + Environment.NewLine; } matlabScript += "title('" + currentDescription + "', 'FontWeight','bold', 'FontUnits', 'normalized', 'FontSize', 0.03);" + Environment.NewLine; matlabScript += "ylabel(gca, '" + yAxisNote + "', 'FontWeight','bold', 'FontUnits', 'normalized', 'FontSize', 0.03);" + Environment.NewLine; matlabScript += "xlabel(gca, '" + xAxisNote + "', 'FontWeight','bold', 'FontUnits', 'normalized', 'FontSize', 0.03);" + Environment.NewLine; matlabScript += "export_fig '" + filename + "';" + Environment.NewLine; matlabScript += "close(fig);" + Environment.NewLine; ServiceTools.logToTextFile(filename.Replace(".", "") + ".m", matlabScript); //MLApp.MLApp ml = new MLApp.MLApp(); //ml.Visible = 0; //ml.Execute("run('" + curDir + "tmpMLscript.m" + "');"); //File.Delete(curDir + "tmpMLscript.m"); //ml.Execute("exit;"); }
/// <summary> /// Run example /// </summary> public void Run() { // Format vector output to console var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone(); formatProvider.TextInfo.ListSeparator = " "; // Create new empty vector var vectorA = new DenseVector(10); Console.WriteLine(@"Empty vector A"); Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 1. Fill vector by data using indexer [] for (var i = 0; i < vectorA.Count; i++) { vectorA[i] = i; } Console.WriteLine(@"1. Fill vector by data using indexer []"); Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 2. Fill vector by data using SetValues method vectorA.SetValues(new[] { 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0 }); Console.WriteLine(@"2. Fill vector by data using SetValues method"); Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 3. Convert Vector to double[] var data = vectorA.ToArray(); Console.WriteLine(@"3. Convert vector to double array"); for (var i = 0; i < data.Length; i++) { Console.Write(data[i].ToString("#0.00\t", formatProvider) + @" "); } Console.WriteLine(); Console.WriteLine(); // 4. Convert Vector to column matrix. A matrix based on this vector in column form (one single column) var columnMatrix = vectorA.ToColumnMatrix(); Console.WriteLine(@"4. Convert vector to column matrix"); Console.WriteLine(columnMatrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 5. Convert Vector to row matrix. A matrix based on this vector in row form (one single row) var rowMatrix = vectorA.ToRowMatrix(); Console.WriteLine(@"5. Convert vector to row matrix"); Console.WriteLine(rowMatrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 6. Clone vector var cloneA = vectorA.Clone(); Console.WriteLine(@"6. Clone vector"); Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 7. Clear vector cloneA.Clear(); Console.WriteLine(@"7. Clear vector"); Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 8. Copy part of vector into another vector. If you need to copy all data then use CopoTy(vector) method. vectorA.CopySubVectorTo(cloneA, 3, 3, 4); Console.WriteLine(@"8. Copy part of vector into another vector"); Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 9. Get part of vector as another vector var subvector = vectorA.SubVector(0, 5); Console.WriteLine(@"9. Get subvector"); Console.WriteLine(subvector.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 10. Enumerator usage Console.WriteLine(@"10. Enumerator usage"); foreach (var value in vectorA) { Console.Write(value.ToString("#0.00\t", formatProvider) + @" "); } Console.WriteLine(); Console.WriteLine(); // 11. Indexed enumerator usage Console.WriteLine(@"11. Enumerator usage"); foreach (var value in vectorA.EnumerateIndexed()) { Console.WriteLine(@"Index = {0}; Value = {1}", value.Item1, value.Item2.ToString("#0.00\t", formatProvider)); } Console.WriteLine(); Console.WriteLine(); // 12. Indexed non-zero enumerator usage Console.WriteLine(@"11. Non-Zero Enumerator usage"); foreach (var value in vectorA.EnumerateNonZeroIndexed()) { Console.WriteLine(@"Index = {0}; Value = {1}", value.Item1, value.Item2.ToString("#0.00\t", formatProvider)); } Console.WriteLine(); }
public static DenseVector NPolynomeApproximationLessSquareMethod(DenseVector dvDataValues, DenseVector dvSpace, DenseVector dvFixedValues = null, int polynomeOrder = 3) { double[] koeffOut = new double[polynomeOrder + 1]; int pointsCount = dvSpace.Count; DenseMatrix dmFactorsA = DenseMatrix.Create(polynomeOrder + 1, polynomeOrder + 1, new Func <int, int, double>( (row, column) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += Math.Pow(dvSpace[j], (column + row)); } return(sum); })); DenseVector dvFactorB = DenseVector.Create(polynomeOrder + 1, new Func <int, double>( (index) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += dvDataValues[j] * Math.Pow(dvSpace[j], index); } return(sum); })); if (dvFixedValues != null) { int conditionIndex = 0; foreach (Tuple <int, double> fixedValueTuple in dvFixedValues.EnumerateIndexed()) { int fixedValueIndex = fixedValueTuple.Item1; double fixedValue = fixedValueTuple.Item2; if (!double.IsNaN(fixedValue)) { conditionIndex++; //int modificationRowIndex = dmFactorsA.RowCount - conditionIndex; int modificationRowIndex = conditionIndex - 1; dmFactorsA.MapIndexedInplace(new Func <int, int, double, double>((row, column, dValue) => { if (row == modificationRowIndex) { return(Math.Pow(dvSpace[fixedValueIndex], column)); } else { return(dValue); } })); dvFactorB.MapIndexedInplace(new Func <int, double, double>((index, dValue) => { if (index == modificationRowIndex) { return(fixedValue); } else { return(dValue); } })); } } } DenseVector dvResult = (DenseVector)dmFactorsA.LU().Solve(dvFactorB); return(dvResult); }