Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ install:
- sh: sudo apt-get -y install libblas-dev liblapack-dev
build: off
test_script:
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/ExecuteUnitTests.ps1')"
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/GenerateCode.ps1')"
- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/ExecuteUnitTests.ps1')"
84 changes: 84 additions & 0 deletions GenerateCode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# first check if T4 engine is installed
if ( -not ( Get-Command t4 -errorAction SilentlyContinue))
{
Write-Host "T4 tool was not found - will be installed."
dotnet tool install -g dotnet-t4
Write-Host "T4 has been installed."
}
else
{
Write-Host "T4 tool is already installed."
}

Write-Host ("location of T4 tool is : " + (Get-Command t4).Path);
Write-Host "----------------------------------------------------"
Write-Host "----------------------------------------------------"
Write-Host ("Test T4 before using it.");

# set variables
$projectFolders = @{};
$projectFolders['projectRoot'] = $PSScriptRoot;
$projectFolders['tmp'] = $projectFolders['projectRoot'] + '/tmp_' + [System.Guid]::NewGuid();
$projectFolders['tt.elementwise'] = $projectFolders['projectRoot'] + "/src/NumSharp.Core/Operations/NdArray.ElementsWise.tt";

Write-Host ("new tmp folder at " + $projectFolders["tmp"])

$projectFolders['tmp.tt'] = $projectFolders['tmp'] + '/test.tt';
$projectFolders['tmp.html'] = $projectFolders['tmp'] + '/date.html';

New-Item -ItemType Directory -Path $projectFolders['tmp'];

'<html><body>' > $projectFolders['tmp.tt'];
'The date and time now is: <#= DateTime.Now #>' >> $projectFolders['tmp.tt'];
'</body></html>' >> $projectFolders['tmp.tt'];

Write-Host "";
Write-Host ("folder " + $projectFolders["tmp"] + " was created.");
Write-Host "";

t4 -o $projectFolders['tmp.html'] $projectFolders['tmp.tt']

if (Test-Path -Path ($projectFolders['tmp.html']))
{
Write-Host "html doc exist - was generated from tt."
Write-Host "Everything should be fine..."
}

Write-Host ""
Write-Host "Tidy up now."
Write-Host ""

Remove-Item -Recurse -Path $projectFolders["tmp"]

Write-Host "Start true Code-Generation.";
Write-Host "";
Write-Host "Generate element wise operations + , - , * , /";
Write-Host "";

$supportDataType = New-Object 'System.Collections.Generic.List[System.String]';

$supportDataType.Add('System.Int32');
$supportDataType.Add('System.Int64');
$supportDataType.Add('System.Single');
$supportDataType.Add('System.Double');
$supportDataType.Add('System.Numerics.Complex');
$supportDataType.Add('System.Numerics.Quaternion');


$operationTypeString = [System.String]::Join(';',$supportDataType);

$command = "t4 -o - -p:operationName='+' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Addition.cs";
Write-Host ("execute - " + $command);
Invoke-Expression $command;

$command = "t4 -o - -p:operationName='*' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Multiplication.cs";
Write-Host ("execute - " + $command);
Invoke-Expression $command;

$command = "t4 -o - -p:operationName='/' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Division.cs";
Write-Host ("execute - " + $command);
Invoke-Expression $command;

$command = "t4 -o - -p:operationName='-' -p:operationTypesString='" + $operationTypeString + "' " + $projectFolders['tt.elementwise'] + " > " + $projectFolders['projectRoot'] + "\src\NumSharp.Core\Operations\Elementwise\NdArray.Substraction.cs";
Write-Host ("execute - " + $command);
Invoke-Expression $command;
49 changes: 22 additions & 27 deletions src/NumSharp.Core/Casting/NdArray.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,29 @@ public override string ToString()

if (this.ndim == 2)
{
if(dtype == typeof(int))
{
output = this._ToMatrixString<int>();
}
else if(dtype == typeof(double))
{
output = this._ToMatrixString<double>();
}

output = this._ToMatrixString();
}
else
{
if (dtype == typeof(int))
{
//output = this._ToVectorString<int>();
}
else if (dtype == typeof(double))
{
//output = this._ToVectorString<double>();
}
output = this._ToVectorString();
}

return output;
}
protected string _ToVectorString<T>()
protected string _ToVectorString()
{
string returnValue = "array([";

int digitBefore = 0;
int digitAfter = 0;

var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
string[] dataParsed = new string[Storage.GetData().Length];

Array strg = Storage.GetData();

for (int idx = 0; idx < dataParsed.Length;idx++)
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);

string elementFormatStart = "{0:";

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

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

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

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

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

return returnValue;
}
protected string _ToMatrixString<T>()
protected string _ToMatrixString()
{
string returnValue = "array([[";

int digitBefore = 0;
int digitAfter = 0;

string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray();
string[] dataParsed = new string[Storage.GetData().Length];

Array strg = Storage.GetData();

for (int idx = 0; idx < dataParsed.Length;idx++)
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);

string elementFormatStart = "{0:";

string elementFormatEnd = "";
Expand All @@ -94,27 +89,27 @@ protected string _ToMatrixString<T>()
int missingDigits;
string elementFormat;

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

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

if (((idx + 1) % shape[1]) == 0)
{
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n [");
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + "], \n [");
}
else
{
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", ");
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + ", ");
}
}

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

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

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

return returnValue;
}
Expand Down
Loading