|
| 1 | +/* |
| 2 | + * NumSharp |
| 3 | + * Copyright (C) 2018 Haiping Chen |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the Apache License 2.0 as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the Apache License 2.0 |
| 16 | + * along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0/>. |
| 17 | + */ |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.ComponentModel; |
| 22 | +using System.Linq; |
| 23 | +using System.Text; |
| 24 | +using System.Globalization; |
| 25 | +using System.Collections; |
| 26 | + |
| 27 | +namespace NumSharp |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// A powerful N-dimensional array object |
| 31 | + /// Inspired from https://www.numpy.org/devdocs/user/quickstart.html |
| 32 | + /// </summary> |
| 33 | + public partial class NDArrayWithDType |
| 34 | + { |
| 35 | + public Type int16 = typeof(short); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// storage by data type |
| 39 | + /// </summary> |
| 40 | + public int[] storageForInt32 { get; set; } |
| 41 | + |
| 42 | + private Shape shape; |
| 43 | + /// <summary> |
| 44 | + /// Data length of every dimension |
| 45 | + /// </summary> |
| 46 | + public Shape Shape |
| 47 | + { |
| 48 | + get |
| 49 | + { |
| 50 | + return shape; |
| 51 | + } |
| 52 | + set |
| 53 | + { |
| 54 | + shape = value; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Dimension count |
| 60 | + /// </summary> |
| 61 | + public int NDim => Shape.Length; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Total of elements |
| 65 | + /// </summary> |
| 66 | + public int Size => Shape.Size; |
| 67 | + |
| 68 | + public NDArrayWithDType() |
| 69 | + { |
| 70 | + // set default shape as 1 dim and 0 elements. |
| 71 | + Shape = new Shape(new int[] { 0 }); |
| 72 | + } |
| 73 | + |
| 74 | + public override string ToString() |
| 75 | + { |
| 76 | + string output = ""; |
| 77 | + |
| 78 | + if (this.NDim == 2) |
| 79 | + { |
| 80 | + output = this._ToMatrixString(); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + output = this._ToVectorString(); |
| 85 | + } |
| 86 | + |
| 87 | + return output; |
| 88 | + } |
| 89 | + |
| 90 | + public override bool Equals(object obj) |
| 91 | + { |
| 92 | + return storageForInt32[0].Equals(obj); |
| 93 | + } |
| 94 | + |
| 95 | + public static bool operator ==(NDArrayWithDType np, object obj) |
| 96 | + { |
| 97 | + return np.storageForInt32[0].Equals(obj); |
| 98 | + } |
| 99 | + |
| 100 | + public static bool operator !=(NDArrayWithDType np, object obj) |
| 101 | + { |
| 102 | + return np.storageForInt32[0].Equals(obj); |
| 103 | + } |
| 104 | + |
| 105 | + public override int GetHashCode() |
| 106 | + { |
| 107 | + unchecked |
| 108 | + { |
| 109 | + var result = 1337; |
| 110 | + result = (result * 397) ^ this.NDim; |
| 111 | + result = (result * 397) ^ this.Size; |
| 112 | + return result; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + protected string _ToVectorString() |
| 117 | + { |
| 118 | + string returnValue = "array(["; |
| 119 | + |
| 120 | + int digitBefore = 0; |
| 121 | + int digitAfter = 0; |
| 122 | + |
| 123 | + var dataParsed = storageForInt32.Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray(); |
| 124 | + |
| 125 | + string elementFormatStart = "{0:"; |
| 126 | + |
| 127 | + string elementFormatEnd = ""; |
| 128 | + for(int idx = 0; idx < digitAfter;idx++) |
| 129 | + elementFormatEnd += "0"; |
| 130 | + |
| 131 | + elementFormatEnd += "}"; |
| 132 | + |
| 133 | + int missingDigits; |
| 134 | + string elementFormat; |
| 135 | + |
| 136 | + for (int idx = 0; idx < (storageForInt32.Length-1);idx++) |
| 137 | + { |
| 138 | + missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length; |
| 139 | + |
| 140 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd; |
| 141 | + |
| 142 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + ", "); |
| 143 | + } |
| 144 | + missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length; |
| 145 | + |
| 146 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd; |
| 147 | + |
| 148 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32.Last()) + "])"); |
| 149 | + |
| 150 | + return returnValue; |
| 151 | + } |
| 152 | + protected string _ToMatrixString() |
| 153 | + { |
| 154 | + string returnValue = "array([["; |
| 155 | + |
| 156 | + int digitBefore = 0; |
| 157 | + int digitAfter = 0; |
| 158 | + |
| 159 | + var dataParsed = storageForInt32.Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray(); |
| 160 | + |
| 161 | + string elementFormatStart = "{0:"; |
| 162 | + |
| 163 | + string elementFormatEnd = ""; |
| 164 | + for(int idx = 0; idx < digitAfter;idx++) |
| 165 | + elementFormatEnd += "0"; |
| 166 | + |
| 167 | + elementFormatEnd += "}"; |
| 168 | + |
| 169 | + int missingDigits; |
| 170 | + string elementFormat; |
| 171 | + |
| 172 | + for (int idx = 0; idx < (storageForInt32.Length-1);idx++) |
| 173 | + { |
| 174 | + missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length; |
| 175 | + |
| 176 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd; |
| 177 | + |
| 178 | + if( ((idx+1) % Shape.Shapes[1] ) == 0 ) |
| 179 | + { |
| 180 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + "], \n ["); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + ", "); |
| 185 | + } |
| 186 | + |
| 187 | + } |
| 188 | + missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length; |
| 189 | + |
| 190 | + elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd; |
| 191 | + |
| 192 | + returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32.Last()) + "]])"); |
| 193 | + |
| 194 | + return returnValue; |
| 195 | + } |
| 196 | + protected string _ParseNumber(object number, ref int noBefore,ref int noAfter) |
| 197 | + { |
| 198 | + string parsed = string.Format(new CultureInfo("en-us"),"{0:0.00000000}",number); |
| 199 | + |
| 200 | + parsed = (parsed.StartsWith("-")) ? parsed : (" " + parsed); |
| 201 | + |
| 202 | + int noBefore_local = parsed.Split('.')[0].Length; |
| 203 | + int noAfter_local = parsed.Split('.')[1].ToCharArray().Reverse().SkipWhile(x => x == '0').ToArray().Length; |
| 204 | + |
| 205 | + noBefore = (noBefore_local > noBefore) ? noBefore_local : noBefore; |
| 206 | + noAfter = (noAfter_local > noAfter ) ? noAfter_local : noAfter; |
| 207 | + |
| 208 | + return parsed; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments