|
| 1 | +using System; |
| 2 | +using NumSharp.Core; |
| 3 | +using System.Linq; |
| 4 | +using System.Globalization; |
| 5 | +using System.Collections.Generic; |
| 6 | + |
| 7 | +namespace NumSharp.Core |
| 8 | +{ |
| 9 | + public partial class NDArray |
| 10 | + { |
| 11 | + public override string ToString() |
| 12 | + { |
| 13 | + string output = ""; |
| 14 | + |
| 15 | + if (this.ndim == 2) |
| 16 | + { |
| 17 | + if(dtype == typeof(int)) |
| 18 | + { |
| 19 | + output = this._ToMatrixString<int>(); |
| 20 | + } |
| 21 | + else if(dtype == typeof(double)) |
| 22 | + { |
| 23 | + output = this._ToMatrixString<double>(); |
| 24 | + } |
| 25 | + |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + if (dtype == typeof(int)) |
| 30 | + { |
| 31 | + //output = this._ToVectorString<int>(); |
| 32 | + } |
| 33 | + else if (dtype == typeof(double)) |
| 34 | + { |
| 35 | + //output = this._ToVectorString<double>(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return output; |
| 40 | + } |
| 41 | + protected string _ToVectorString<T>() |
| 42 | + { |
| 43 | + string returnValue = "array(["; |
| 44 | + |
| 45 | + int digitBefore = 0; |
| 46 | + int digitAfter = 0; |
| 47 | + |
| 48 | + var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray(); |
| 49 | + |
| 50 | + string elementFormatStart = "{0:"; |
| 51 | + |
| 52 | + string elementFormatEnd = ""; |
| 53 | + for(int idx = 0; idx < digitAfter;idx++) |
| 54 | + elementFormatEnd += "0"; |
| 55 | + |
| 56 | + elementFormatEnd += "}"; |
| 57 | + |
| 58 | + int missingDigits; |
| 59 | + string elementFormat; |
| 60 | + |
| 61 | + for (int idx = 0; idx < (Storage.Shape.Size-1);idx++) |
| 62 | + { |
| 63 | + missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length; |
| 64 | + |
| 65 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd; |
| 66 | + |
| 67 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>()[idx]) + ", "); |
| 68 | + } |
| 69 | + missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length; |
| 70 | + |
| 71 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd; |
| 72 | + |
| 73 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "])"); |
| 74 | + |
| 75 | + return returnValue; |
| 76 | + } |
| 77 | + protected string _ToMatrixString<T>() |
| 78 | + { |
| 79 | + string returnValue = "array([["; |
| 80 | + |
| 81 | + int digitBefore = 0; |
| 82 | + int digitAfter = 0; |
| 83 | + |
| 84 | + string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray(); |
| 85 | + |
| 86 | + string elementFormatStart = "{0:"; |
| 87 | + |
| 88 | + string elementFormatEnd = ""; |
| 89 | + for(int idx = 0; idx < digitAfter;idx++) |
| 90 | + elementFormatEnd += "0"; |
| 91 | + |
| 92 | + elementFormatEnd += "}"; |
| 93 | + |
| 94 | + int missingDigits; |
| 95 | + string elementFormat; |
| 96 | + |
| 97 | + for (int idx = 0; idx < shape.NDim - 1; idx++) |
| 98 | + { |
| 99 | + missingDigits = digitBefore - dataParsed[idx].Replace(" ", "").Split('.')[0].Length; |
| 100 | + |
| 101 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ', missingDigits).ToArray()) + "0." + elementFormatEnd; |
| 102 | + |
| 103 | + if (((idx + 1) % shape.Dimensions[1]) == 0) |
| 104 | + { |
| 105 | + returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n ["); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", "); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length; |
| 114 | + |
| 115 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd; |
| 116 | + |
| 117 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "]])"); |
| 118 | + |
| 119 | + return returnValue; |
| 120 | + } |
| 121 | + protected string _ParseNumber(object number, ref int noBefore,ref int noAfter) |
| 122 | + { |
| 123 | + string parsed = string.Format(new CultureInfo("en-us"),"{0:0.00000000}",number); |
| 124 | + |
| 125 | + parsed = (parsed.StartsWith("-")) ? parsed : (" " + parsed); |
| 126 | + |
| 127 | + int noBefore_local = parsed.Split('.')[0].Length; |
| 128 | + int noAfter_local = parsed.Split('.')[1].ToCharArray().Reverse().SkipWhile(x => x == '0').ToArray().Length; |
| 129 | + |
| 130 | + noBefore = (noBefore_local > noBefore) ? noBefore_local : noBefore; |
| 131 | + noAfter = (noAfter_local > noAfter ) ? noAfter_local : noAfter; |
| 132 | + |
| 133 | + return parsed; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
| 138 | + |
0 commit comments