/// <summary> /// Formats a <see cref="Complex"/> object to produce a string. /// </summary> /// <param name="complex">the object to format.</param> /// <param name="toAppendTo">where the text is to be appended</param> /// <returns>the value passed in as toAppendTo.</returns> public StringBuilder format(Complex complex, StringBuilder toAppendTo) { CultureInfo Real = CultureInfo.CurrentCulture; Real.NumberFormat = getRealFormat(); // format real double re = complex.getReal(); CompositeFormat.formatDouble(re, Real, toAppendTo); // format sign and imaginary double im = complex.getImaginary(); StringBuilder imAppendTo; if (im < 0.0) { toAppendTo.Append(" - "); imAppendTo = formatImaginary(-im, new StringBuilder()); toAppendTo.Append(imAppendTo); toAppendTo.Append(getImaginaryCharacter()); } else if (im > 0.0 || Double.IsNaN(im)) { toAppendTo.Append(" + "); imAppendTo = formatImaginary(im, new StringBuilder()); toAppendTo.Append(imAppendTo); toAppendTo.Append(getImaginaryCharacter()); } return(toAppendTo); }
/// <summary> /// Formats a <see cref="RealMatrix"/> object to produce a string. /// </summary> /// <param name="matrix">the object to format.</param> /// <param name="toAppendTo">where the text is to be appended</param> /// <returns>the value passed in as toAppendTo.</returns> public StringBuilder format(RealMatrix matrix, StringBuilder toAppendTo) { // format prefix toAppendTo.Append(prefix); // format rows int rows = matrix.getRowDimension(); for (int i = 0; i < rows; ++i) { toAppendTo.Append(rowPrefix); for (int j = 0; j < matrix.getColumnDimension(); ++j) { if (j > 0) { toAppendTo.Append(columnSeparator); } CompositeFormat.formatDouble(matrix.getEntry(i, j), CultureInfo.CurrentCulture, toAppendTo); } toAppendTo.Append(rowSuffix); if (i < rows - 1) { toAppendTo.Append(rowSeparator); } } // format suffix toAppendTo.Append(suffix); return(toAppendTo); }
/// <summary> /// Parse a string to produce a <see cref="RealVector"/> object. /// </summary> /// <param name="source">String to parse.</param> /// <param name="pos">input/ouput parsing parameter.</param> /// <returns>the parsed <see cref="RealVector"/> object.</returns> public ArrayRealVector parse(String source, Int32 pos) { int initialIndex = pos; // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix)) { return(null); } // parse components List <double> components = new List <double>(); for (Boolean loop = true; loop;) { if (!(components.Count == 0)) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSeparator)) { loop = false; } } if (loop) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); double component = CompositeFormat.parseNumber(source, CultureInfo.CurrentCulture); if (Double.IsNaN(component)) { components.Add(component); } else { // invalid component // set index back to initial, error index should already be set pos = initialIndex; return(null); } } } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix)) { return(null); } // build vector double[] data = new double[components.Count]; for (int i = 0; i < data.Length; ++i) { data[i] = components[i]; } return(new ArrayRealVector(data, false)); }
private static void CheckFormatWithSpan(string?expectedResult, string format, params object?[]?args) { var cf = new CompositeFormat(format); var s = new Span <char>(new char[(format.Length * 2) + 128]); Assert.True(cf.TryFormat(s, out int charsWritten, null, args)); var actualResult = s.Slice(0, charsWritten).ToString(); Assert.Equal(expectedResult, actualResult); }
/// <summary> /// Format the absolute value of the imaginary part. /// </summary> /// <param name="absIm">Absolute value of the imaginary part of a complex number.</param> /// <param name="toAppendTo">where the text is to be appended.</param> /// <returns>the value passed in as toAppendTo.</returns> private StringBuilder formatImaginary(double absIm, StringBuilder toAppendTo) { CultureInfo Imag = CultureInfo.CurrentCulture; Imag.NumberFormat = getRealFormat(); CompositeFormat.formatDouble(absIm, Imag, toAppendTo); if (toAppendTo.ToString().Equals("1")) { // Remove the character "1" if it is the only one. toAppendTo.Length = 0; } return(toAppendTo); }
private static void CheckFormatWithString <T0, T1, T2>(string?expectedResult, string format, T0 arg0, T1 arg1, T2 arg2) { var cf = new CompositeFormat(format); var actualResult1 = cf.Format(null, arg0, arg1, arg2); var actualResult2 = cf.Format(arg0, arg1, arg2); var actualResult3 = new StringBuilder().AppendFormat(cf, null, arg0, arg1, arg2).ToString(); var actualResult4 = new StringBuilder().AppendFormat(cf, arg0, arg1, arg2).ToString(); Assert.Equal(expectedResult, actualResult1); Assert.Equal(expectedResult, actualResult2); Assert.Equal(expectedResult, actualResult3); Assert.Equal(expectedResult, actualResult4); }
private static void CheckFormatWithString(string?expectedResult, string format, params object?[]?args) { var cf = new CompositeFormat(format); var actualResult1 = cf.Format(null, args); var actualResult2 = cf.Format(args); var actualResult3 = new StringBuilder().AppendFormat(cf, null, args).ToString(); var actualResult4 = new StringBuilder().AppendFormat(cf, args).ToString(); Assert.Equal(expectedResult, actualResult1); Assert.Equal(expectedResult, actualResult2); Assert.Equal(expectedResult, actualResult3); Assert.Equal(expectedResult, actualResult4); }
private static void CheckExpansion <T>(T arg) { var format = "{0,256} {1}"; var expectedResult = string.Format(format, 3.14, arg); var cf = new CompositeFormat(format); var actualResult1 = cf.Format(null, 3.14, arg); var actualResult2 = cf.Format(3.14, arg); var actualResult3 = new StringBuilder().AppendFormat(cf, null, 3.14, arg).ToString(); var actualResult4 = new StringBuilder().AppendFormat(cf, 3.14, arg).ToString(); Assert.Equal(expectedResult, actualResult1); Assert.Equal(expectedResult, actualResult2); Assert.Equal(expectedResult, actualResult3); Assert.Equal(expectedResult, actualResult4); }
/// <summary> /// Formats a <see cref="RealVector"/> object to produce a string. /// </summary> /// <param name="vector">the object to format.</param> /// <param name="toAppendTo">where the text is to be appended</param> /// <returns>the value passed in as toAppendTo.</returns> public StringBuilder format(RealVector vector, StringBuilder toAppendTo) { // format prefix toAppendTo.Append(prefix); // format components for (int i = 0; i < vector.getDimension(); ++i) { if (i > 0) { toAppendTo.Append(separator); } CompositeFormat.formatDouble(vector.getEntry(i), CultureInfo.CurrentCulture, toAppendTo); } // format suffix toAppendTo.Append(suffix); return(toAppendTo); }
/// <summary> /// Parses a string to produce a <see cref="Complex"/> object. /// </summary> /// <param name="source">the string to parse</param> /// <param name="pos">input/ouput parsing parameter.</param> /// <returns>the parsed <c>Complex</c> object.</returns> public Complex parse(String source, Int32 pos) { int initialIndex = pos; // parse whitespace Int32 nonSpace = CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse real Double re = CompositeFormat.parseNumber(source, CultureInfo.CurrentCulture); if (Double.IsNaN(re)) { // invalid real number // set index back to initial, error index should already be set return(null); } // parse sign Int32 p = CompositeFormat.parseNextCharacter(source, nonSpace); char c = source[p]; int sign = 0; switch (c) { case '0': // no sign // return real only complex number return(new Complex(re, 0.0)); case '-': sign = -1; break; case '+': sign = 1; break; default: // invalid sign // set index back to initial, error index should be the last // character examined. return(null); } // parse whitespace CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse imaginary Double im = CompositeFormat.parseNumber(source, CultureInfo.CurrentCulture); if (Double.IsNaN(im)) { // invalid imaginary number // set index back to initial, error index should already be set return(null); } // parse imaginary character if (!CompositeFormat.parseFixedstring(source, getImaginaryCharacter())) { return(null); } return(new Complex(re, im * sign)); }
/// <summary> /// Create an instance with default settings. /// <para>The instance uses the default prefix, suffix and separator: /// "{", "}", and "; " and the default number format for components.</para> /// </summary> public RealVectorFormat() : this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()) { }
/// <summary> /// Parse a string to produce a <see cref="RealMatrix"/> object. /// </summary> /// <param name="source">String to parse.</param> /// <param name="pos">input/ouput parsing parameter.</param> /// <returns>the parsed <see cref="RealMatrix"/> object.</returns> public RealMatrix parse(String source, ref Int32 pos) { int initialIndex = pos; String trimmedPrefix = prefix.Trim(); String trimmedSuffix = suffix.Trim(); String trimmedRowPrefix = rowPrefix.Trim(); String trimmedRowSuffix = rowSuffix.Trim(); String trimmedColumnSeparator = columnSeparator.Trim(); String trimmedRowSeparator = rowSeparator.Trim(); // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix)) { return(null); } // parse components List <List <double> > matrix = new List <List <double> >(); List <double> rowComponents = new List <double>(); for (Boolean loop = true; loop;) { if (rowComponents.Count != 0) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedColumnSeparator)) { if (trimmedRowSuffix.Length != 0 && !CompositeFormat.parseFixedstring(source, trimmedRowSuffix)) { return(null); } else { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (CompositeFormat.parseFixedstring(source, trimmedRowSeparator)) { matrix.Add(rowComponents); rowComponents = new List <double>(); continue; } else { loop = false; } } } } else { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (trimmedRowPrefix.Length != 0 && !CompositeFormat.parseFixedstring(source, trimmedRowPrefix)) { return(null); } } if (loop) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); double component = CompositeFormat.parseNumber(source, CultureInfo.CurrentCulture); if (Double.IsNaN(component)) { rowComponents.Add(component); } else { if (rowComponents.Count == 0) { loop = false; } else { // invalid component // set index back to initial, error index should already be set pos = initialIndex; return(null); } } } } if (rowComponents.Count != 0) { matrix.Add(rowComponents); } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix)) { return(null); } // do not allow an empty matrix if (matrix.Count == 0) { pos = initialIndex; return(null); } // build vector double[][] data = new double[matrix.Count][]; int row = 0; foreach (List <double> rowList in matrix) { data[row] = new double[rowList.Count]; for (int i = 0; i < rowList.Count; i++) { data[row][i] = rowList[i]; } row++; } return(MatrixUtils.createRealMatrix(data)); }
/// <summary> /// Create an instance with a custom imaginary character, and the default /// number format for both real and imaginary parts. /// </summary> /// <param name="imaginaryCharacter">The custom imaginary character.</param> /// <exception cref="NullArgumentException"> if <c>imaginaryCharacter</c> is /// <c>null</c>.</exception> /// <exception cref="NoDataException"> if <c>imaginaryCharacter</c> is an /// empty string.</exception> public ComplexFormat(String imaginaryCharacter) : this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat()) { }
/// <summary> /// Returns the default real vector format for the given locale. /// </summary> /// <param name="locale">the specific locale used by the format.</param> /// <returns>the real vector format specific to the given locale.</returns> public static RealMatrixFormat getInstance(CultureInfo locale) { return(new RealMatrixFormat(CompositeFormat.getDefaultNumberFormat(locale))); }
/// <summary> /// Create an instance with custom prefix, suffix and separator. /// </summary> /// <param name="prefix">prefix to use instead of the default "{"</param> /// <param name="suffix">suffix to use instead of the default "}"</param> /// <param name="rowPrefix">row prefix to use instead of the default "{"</param> /// <param name="rowSuffix">row suffix to use instead of the default "}"</param> /// <param name="rowSeparator">tow separator to use instead of the default ";"</param> /// <param name="columnSeparator">column separator to use instead of the default ", "</param> public RealMatrixFormat(String prefix, String suffix, String rowPrefix, String rowSuffix, String rowSeparator, String columnSeparator) : this(prefix, suffix, rowPrefix, rowSuffix, rowSeparator, columnSeparator, CompositeFormat.getDefaultNumberFormat()) { }
/// <summary> /// Create an instance with default settings. /// <para>The instance uses the default prefix, suffix and row/column separator: /// "[", "]", ";" and ", " and the default number format for components.</para> /// </summary> public RealMatrixFormat() : this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX, DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, CompositeFormat.getDefaultNumberFormat()) { }
/// <summary> /// Returns the default complex format for the given locale. /// </summary> /// <param name="locale">the specific locale used by the format.</param> /// <returns>the complex format specific to the given locale.</returns> public static ComplexFormat getInstance(CultureInfo locale) { NumberFormatInfo f = CompositeFormat.getDefaultNumberFormat(locale); return(new ComplexFormat(f)); }
/// <summary> /// Returns the default complex format for the given locale. /// </summary> /// <param name="imaginaryCharacter">Imaginary character.</param> /// <param name="locale">the specific locale used by the format.</param> /// <returns>the complex format specific to the given locale.</returns> /// <exception cref="NullArgumentException"> if <c>imaginaryCharacter</c> is /// <c>null</c>.</exception> /// <exception cref="NoDataException"> if <c>imaginaryCharacter</c> is an /// empty string.</exception> public static ComplexFormat getInstance(String imaginaryCharacter, CultureInfo locale) { NumberFormatInfo f = CompositeFormat.getDefaultNumberFormat(locale); return(new ComplexFormat(imaginaryCharacter, f)); }
/// <summary> /// Create an instance with the default imaginary character, 'i', and the /// default number format for both real and imaginary parts. /// </summary> public ComplexFormat() { this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER; this.imaginaryFormat = CompositeFormat.getDefaultNumberFormat(); this.realFormat = imaginaryFormat; }
/// <summary> /// Create an instance with custom prefix, suffix and separator. /// </summary> /// <param name="prefix">prefix to use instead of the default "{"</param> /// <param name="suffix">suffix to use instead of the default "}"</param> /// <param name="separator">separator to use instead of the default "; "</param> public RealVectorFormat(String prefix, String suffix, String separator) : this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()) { }