Skip to content

Commit f06e9ad

Browse files
author
dotchris90
committed
Fix : ToString() just showed 2 elements (fix complete)
1 parent db26353 commit f06e9ad

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

src/NumSharp.Core/Casting/NdArray.ToString.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,29 @@ public override string ToString()
1414

1515
if (this.ndim == 2)
1616
{
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-
17+
output = this._ToMatrixString();
2618
}
2719
else
2820
{
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-
}
21+
output = this._ToVectorString();
3722
}
3823

3924
return output;
4025
}
41-
protected string _ToVectorString<T>()
26+
protected string _ToVectorString()
4227
{
4328
string returnValue = "array([";
4429

4530
int digitBefore = 0;
4631
int digitAfter = 0;
4732

48-
var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
33+
string[] dataParsed = new string[Storage.GetData().Length];
34+
35+
Array strg = Storage.GetData();
4936

37+
for (int idx = 0; idx < dataParsed.Length;idx++)
38+
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);
39+
5040
string elementFormatStart = "{0:";
5141

5242
string elementFormatEnd = "";
@@ -64,25 +54,30 @@ protected string _ToVectorString<T>()
6454

6555
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;
6656

67-
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>()[idx]) + ", ");
57+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(idx)) + ", ");
6858
}
6959
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
7060

7161
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
7262

73-
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "])");
63+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(strg.Length-1)) + "])");
7464

7565
return returnValue;
7666
}
77-
protected string _ToMatrixString<T>()
67+
protected string _ToMatrixString()
7868
{
7969
string returnValue = "array([[";
8070

8171
int digitBefore = 0;
8272
int digitAfter = 0;
8373

84-
string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray();
74+
string[] dataParsed = new string[Storage.GetData().Length];
75+
76+
Array strg = Storage.GetData();
8577

78+
for (int idx = 0; idx < dataParsed.Length;idx++)
79+
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);
80+
8681
string elementFormatStart = "{0:";
8782

8883
string elementFormatEnd = "";
@@ -94,27 +89,27 @@ protected string _ToMatrixString<T>()
9489
int missingDigits;
9590
string elementFormat;
9691

97-
for (int idx = 0; idx < this.ndim - 1; idx++)
92+
for (int idx = 0; idx < dataParsed.Length - 1; idx++)
9893
{
9994
missingDigits = digitBefore - dataParsed[idx].Replace(" ", "").Split('.')[0].Length;
10095

10196
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ', missingDigits).ToArray()) + "0." + elementFormatEnd;
10297

10398
if (((idx + 1) % shape[1]) == 0)
10499
{
105-
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n [");
100+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + "], \n [");
106101
}
107102
else
108103
{
109-
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", ");
104+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + ", ");
110105
}
111106
}
112107

113108
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
114109

115110
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
116111

117-
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "]])");
112+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(strg.Length-1)) + "]])");
118113

119114
return returnValue;
120115
}

test/NumSharp.UnitTest/NdArray.Test.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ public class NDArrayTest
1414
[TestMethod]
1515
public void StringCheck()
1616
{
17-
/*
18-
var nd = np.arange(9.0).reshape(3,3);
17+
18+
var nd = np.arange(9.0).reshape(3,3).MakeGeneric<double>();
1919

2020
var random = new Random();
21-
nd.Set(nd.Data<double>().Select(x => x + random.NextDouble()).ToArray());
21+
nd.Storage.SetData(nd.Data<double>().Select(x => x + random.NextDouble()).ToArray());
2222
nd[1,0] = 1.0;
2323
nd[0,0] = 9.0;
2424
nd[2,2] = 7.0;
25-
//nd.Storage[3] -= 20;
26-
//nd.Storage[8] += 23;
25+
nd[0,2] = nd[0,2] - 20.0;
26+
nd[2,2] += 23;
2727

2828
var stringOfNp = nd.ToString();
2929

30-
/*Assert.IsTrue(stringOfNp.Contains("[[ 0."));
30+
Assert.IsTrue(stringOfNp.Contains("[["));
3131

32-
nd = np.arange(9).reshape(3,3);
32+
nd = np.arange(9).reshape(3,3).MakeGeneric<double>();
3333

3434
stringOfNp = nd.ToString();
3535

36-
Assert.IsTrue(stringOfNp.Contains("([[ 0,"));*/
36+
Assert.IsTrue(stringOfNp.Contains("([[ 0,"));
3737

3838
}
3939
[TestMethod]

0 commit comments

Comments
 (0)