Skip to content

Commit a494ed4

Browse files
authored
Merge pull request #188 from dotChris90/master
Add code generation and fixed ToString error
2 parents 351e6ce + f06e9ad commit a494ed4

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
@@ -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
}

0 commit comments

Comments
 (0)