Skip to content

Commit 835e8c3

Browse files
committed
Merge branch 'master' of https://github.com/Oceania2018/NumSharp
2 parents ac3dfa7 + a494ed4 commit 835e8c3

File tree

9 files changed

+1067
-501
lines changed

9 files changed

+1067
-501
lines changed

.appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ install:
88
- sh: sudo apt-get -y install libblas-dev liblapack-dev
99
build: off
1010
test_script:
11-
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/ExecuteUnitTests.ps1')"
11+
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/GenerateCode.ps1')"
12+
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/ExecuteUnitTests.ps1')"

GenerateCode.ps1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# first check if T4 engine is installed
2+
if ( -not ( Get-Command t4 -errorAction SilentlyContinue))
3+
{
4+
Write-Host "T4 tool was not found - will be installed."
5+
dotnet tool install -g dotnet-t4
6+
Write-Host "T4 has been installed."
7+
}
8+
else
9+
{
10+
Write-Host "T4 tool is already installed."
11+
}
12+
13+
Write-Host ("location of T4 tool is : " + (Get-Command t4).Path);
14+
Write-Host "----------------------------------------------------"
15+
Write-Host "----------------------------------------------------"
16+
Write-Host ("Test T4 before using it.");
17+
18+
# set variables
19+
$projectFolders = @{};
20+
$projectFolders['projectRoot'] = $PSScriptRoot;
21+
$projectFolders['tmp'] = $projectFolders['projectRoot'] + '/tmp_' + [System.Guid]::NewGuid();
22+
$projectFolders['tt.elementwise'] = $projectFolders['projectRoot'] + "/src/NumSharp.Core/Operations/NdArray.ElementsWise.tt";
23+
24+
Write-Host ("new tmp folder at " + $projectFolders["tmp"])
25+
26+
$projectFolders['tmp.tt'] = $projectFolders['tmp'] + '/test.tt';
27+
$projectFolders['tmp.html'] = $projectFolders['tmp'] + '/date.html';
28+
29+
New-Item -ItemType Directory -Path $projectFolders['tmp'];
30+
31+
'<html><body>' > $projectFolders['tmp.tt'];
32+
'The date and time now is: <#= DateTime.Now #>' >> $projectFolders['tmp.tt'];
33+
'</body></html>' >> $projectFolders['tmp.tt'];
34+
35+
Write-Host "";
36+
Write-Host ("folder " + $projectFolders["tmp"] + " was created.");
37+
Write-Host "";
38+
39+
t4 -o $projectFolders['tmp.html'] $projectFolders['tmp.tt']
40+
41+
if (Test-Path -Path ($projectFolders['tmp.html']))
42+
{
43+
Write-Host "html doc exist - was generated from tt."
44+
Write-Host "Everything should be fine..."
45+
}
46+
47+
Write-Host ""
48+
Write-Host "Tidy up now."
49+
Write-Host ""
50+
51+
Remove-Item -Recurse -Path $projectFolders["tmp"]
52+
53+
Write-Host "Start true Code-Generation.";
54+
Write-Host "";
55+
Write-Host "Generate element wise operations + , - , * , /";
56+
Write-Host "";
57+
58+
$supportDataType = New-Object 'System.Collections.Generic.List[System.String]';
59+
60+
$supportDataType.Add('System.Int32');
61+
$supportDataType.Add('System.Int64');
62+
$supportDataType.Add('System.Single');
63+
$supportDataType.Add('System.Double');
64+
$supportDataType.Add('System.Numerics.Complex');
65+
$supportDataType.Add('System.Numerics.Quaternion');
66+
67+
68+
$operationTypeString = [System.String]::Join(';',$supportDataType);
69+
70+
$command = "t4 -o - -p:operationName='+' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Addition.cs";
71+
Write-Host ("execute - " + $command);
72+
Invoke-Expression $command;
73+
74+
$command = "t4 -o - -p:operationName='*' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Multiplication.cs";
75+
Write-Host ("execute - " + $command);
76+
Invoke-Expression $command;
77+
78+
$command = "t4 -o - -p:operationName='/' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Division.cs";
79+
Write-Host ("execute - " + $command);
80+
Invoke-Expression $command;
81+
82+
$command = "t4 -o - -p:operationName='-' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Substraction.cs";
83+
Write-Host ("execute - " + $command);
84+
Invoke-Expression $command;

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

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,29 @@ public override string ToString()
3333
}
3434
else if (this.ndim == 2)
3535
{
36-
if(dtype == typeof(int))
37-
{
38-
output = this._ToMatrixString<int>();
39-
}
40-
else if(dtype == typeof(double))
41-
{
42-
output = this._ToMatrixString<double>();
43-
}
44-
36+
output = this._ToMatrixString();
4537
}
4638
else
4739
{
48-
if (dtype == typeof(int))
49-
{
50-
//output = this._ToVectorString<int>();
51-
}
52-
else if (dtype == typeof(double))
53-
{
54-
//output = this._ToVectorString<double>();
55-
}
40+
output = this._ToVectorString();
5641
}
5742

5843
return output;
5944
}
60-
protected string _ToVectorString<T>()
45+
protected string _ToVectorString()
6146
{
6247
string returnValue = "array([";
6348

6449
int digitBefore = 0;
6550
int digitAfter = 0;
6651

67-
var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
52+
string[] dataParsed = new string[Storage.GetData().Length];
53+
54+
Array strg = Storage.GetData();
6855

56+
for (int idx = 0; idx < dataParsed.Length;idx++)
57+
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);
58+
6959
string elementFormatStart = "{0:";
7060

7161
string elementFormatEnd = "";
@@ -83,25 +73,30 @@ protected string _ToVectorString<T>()
8373

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

86-
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>()[idx]) + ", ");
76+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(idx)) + ", ");
8777
}
8878
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
8979

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

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

9484
return returnValue;
9585
}
96-
protected string _ToMatrixString<T>()
86+
protected string _ToMatrixString()
9787
{
9888
string returnValue = "array([[";
9989

10090
int digitBefore = 0;
10191
int digitAfter = 0;
10292

103-
string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray();
93+
string[] dataParsed = new string[Storage.GetData().Length];
94+
95+
Array strg = Storage.GetData();
10496

97+
for (int idx = 0; idx < dataParsed.Length;idx++)
98+
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);
99+
105100
string elementFormatStart = "{0:";
106101

107102
string elementFormatEnd = "";
@@ -113,27 +108,27 @@ protected string _ToMatrixString<T>()
113108
int missingDigits;
114109
string elementFormat;
115110

116-
for (int idx = 0; idx < this.ndim - 1; idx++)
111+
for (int idx = 0; idx < dataParsed.Length - 1; idx++)
117112
{
118113
missingDigits = digitBefore - dataParsed[idx].Replace(" ", "").Split('.')[0].Length;
119114

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

122117
if (((idx + 1) % shape[1]) == 0)
123118
{
124-
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n [");
119+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + "], \n [");
125120
}
126121
else
127122
{
128-
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", ");
123+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + ", ");
129124
}
130125
}
131126

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

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

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

138133
return returnValue;
139134
}

0 commit comments

Comments
 (0)