diff --git a/.appveyor.yml b/.appveyor.yml
index 8b5c374c..40c7241d 100644
--- a/.appveyor.yml
+++ b/.appveyor.yml
@@ -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')"
\ No newline at end of file
+- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/GenerateCode.ps1')"
+- pwsh: "&($env:APPVEYOR_BUILD_FOLDER + '/ExecuteUnitTests.ps1')"
diff --git a/GenerateCode.ps1 b/GenerateCode.ps1
new file mode 100644
index 00000000..98e9fb9c
--- /dev/null
+++ b/GenerateCode.ps1
@@ -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'];
+
+'
' > $projectFolders['tmp.tt'];
+'The date and time now is: <#= DateTime.Now #>' >> $projectFolders['tmp.tt'];
+'' >> $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;
diff --git a/src/NumSharp.Core/Casting/NdArray.ToString.cs b/src/NumSharp.Core/Casting/NdArray.ToString.cs
index 15789893..e57e706d 100644
--- a/src/NumSharp.Core/Casting/NdArray.ToString.cs
+++ b/src/NumSharp.Core/Casting/NdArray.ToString.cs
@@ -14,39 +14,29 @@ public override string ToString()
if (this.ndim == 2)
{
- if(dtype == typeof(int))
- {
- output = this._ToMatrixString();
- }
- else if(dtype == typeof(double))
- {
- output = this._ToMatrixString();
- }
-
+ output = this._ToMatrixString();
}
else
{
- if (dtype == typeof(int))
- {
- //output = this._ToVectorString();
- }
- else if (dtype == typeof(double))
- {
- //output = this._ToVectorString();
- }
+ output = this._ToVectorString();
}
return output;
}
- protected string _ToVectorString()
+ protected string _ToVectorString()
{
string returnValue = "array([";
int digitBefore = 0;
int digitAfter = 0;
- var dataParsed = Storage.GetData().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 = "";
@@ -64,25 +54,30 @@ protected string _ToVectorString()
elementFormat = elementFormatStart + new string(Enumerable.Repeat(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;
- returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData()[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(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
- returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData().Last()) + "])");
+ returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(strg.Length-1)) + "])");
return returnValue;
}
- protected string _ToMatrixString()
+ protected string _ToMatrixString()
{
string returnValue = "array([[";
int digitBefore = 0;
int digitAfter = 0;
- string[] dataParsed = Storage.GetData().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 = "";
@@ -94,7 +89,7 @@ protected string _ToMatrixString()
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;
@@ -102,11 +97,11 @@ protected string _ToMatrixString()
if (((idx + 1) % shape[1]) == 0)
{
- returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData()[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()[idx]) + ", ");
+ returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, strg.GetValue(idx)) + ", ");
}
}
@@ -114,7 +109,7 @@ protected string _ToMatrixString()
elementFormat = elementFormatStart + new string(Enumerable.Repeat(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
- returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData().Last()) + "]])");
+ returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(strg.Length-1)) + "]])");
return returnValue;
}
diff --git a/src/NumSharp.Core/Operations/Elementwise/NdArray.Addition.cs b/src/NumSharp.Core/Operations/Elementwise/NdArray.Addition.cs
index 64b67fab..399bf835 100644
--- a/src/NumSharp.Core/Operations/Elementwise/NdArray.Addition.cs
+++ b/src/NumSharp.Core/Operations/Elementwise/NdArray.Addition.cs
@@ -1,149 +1,231 @@
-using System;
+/*
+This file was generated by template ../NDArray.Elementwise.tt
+In case you want to do some changes do the following
+
+1 ) adapt the tt file
+2 ) execute powershell file "GenerateCode.ps1" on root level
+
+*/
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Numerics;
-
+using NumSharp.Core.Shared;
+
namespace NumSharp.Core
{
public partial class NDArray
{
public static NDArray operator +(NDArray np1, NDArray np2)
- {
- int scalarNo = -1;
-
- var sum = np1.DetermineEmptyResult(np2,ref scalarNo);
+ {
+ /// following code is for determine if scalar or not
+ /// also for determine result
+ int scalarNo = !(np1.ndim == 0 || np2.ndim == 0) ? 0 : -1;
- Array np1SysArr = np1.Storage.GetData();
- Array np2SysArr = np2.Storage.GetData();
- Array np3SysArr = sum.Storage.GetData();
-
- switch (np3SysArr)
+ if( scalarNo == 0 )
{
- case double[] sumArray :
- {
- double[] np1Array = np1SysArr as double[];
- double[] np2Array = np2SysArr as double[];
-
- double scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
- break;
- }
- case float[] sumArray :
+ if (!Enumerable.SequenceEqual(np1.shape,np2.shape))
{
- float[] np1Array = np1SysArr as float[];
- float[] np2Array = np2SysArr as float[];
-
- float scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
- break;
+ throw new IncorrectShapeException();
}
- case int[] sumArray :
+ }
+ else
+ {
+ if (np1.ndim == 0)
+ scalarNo = 1;
+ else
+ scalarNo = 2;
+ }
+
+ NDArray result = null;
+
+ switch (scalarNo)
+ {
+ case 1 :
{
- int[] np1Array = np1SysArr as int[];
- int[] np2Array = np2SysArr as int[];
-
- int scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
+ result = new NDArray(np2.dtype,np2.shape);
break;
}
- case Int64[] sumArray :
+ case 2 :
{
- Int64[] np1Array = np1SysArr as Int64[];
- Int64[] np2Array = np2SysArr as Int64[];
-
- Int64 scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
+ result = new NDArray(np1.dtype,np1.shape);
break;
}
- case Complex[] sumArray :
+ default :
{
- Complex[] np1Array = np1SysArr as Complex[];
- Complex[] np2Array = np2SysArr as Complex[];
-
- Complex scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
+ result = new NDArray(np1.dtype,np1.shape);
break;
- }
- case Quaternion[] sumArray :
- {
- Quaternion[] np1Array = np1SysArr as Quaternion[];
- Quaternion[] np2Array = np2SysArr as Quaternion[];
-
- Quaternion scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] + np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar + np1Array[idx];
- break;
- }
+ }
+ }
+
+ Array np1SysArr = np1.Storage.GetData();
+ Array np2SysArr = np2.Storage.GetData();
+ Array np3SysArr = result.Storage.GetData();
+
+ switch (np3SysArr)
+ {
+
+ case System.Int32[] resArr :
+ {
+ System.Int32[] np1Array = np1SysArr as System.Int32[];
+ System.Int32[] np2Array = np2SysArr as System.Int32[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int32 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int32 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
+
+ case System.Int64[] resArr :
+ {
+ System.Int64[] np1Array = np1SysArr as System.Int64[];
+ System.Int64[] np2Array = np2SysArr as System.Int64[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int64 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int64 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
+
+ case System.Single[] resArr :
+ {
+ System.Single[] np1Array = np1SysArr as System.Single[];
+ System.Single[] np2Array = np2SysArr as System.Single[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Single scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Single scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
+
+ case System.Double[] resArr :
+ {
+ System.Double[] np1Array = np1SysArr as System.Double[];
+ System.Double[] np2Array = np2SysArr as System.Double[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Double scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Double scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Complex[] resArr :
+ {
+ System.Numerics.Complex[] np1Array = np1SysArr as System.Numerics.Complex[];
+ System.Numerics.Complex[] np2Array = np2SysArr as System.Numerics.Complex[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Complex scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Complex scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Quaternion[] resArr :
+ {
+ System.Numerics.Quaternion[] np1Array = np1SysArr as System.Numerics.Quaternion[];
+ System.Numerics.Quaternion[] np2Array = np2SysArr as System.Numerics.Quaternion[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Quaternion scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar + np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Quaternion scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] + scalar;
+ }
+ break;
+ }
default :
{
throw new IncorrectTypeException();
}
}
- return sum;
+ return result;
}
}
}
+
diff --git a/src/NumSharp.Core/Operations/Elementwise/NdArray.Division.cs b/src/NumSharp.Core/Operations/Elementwise/NdArray.Division.cs
index 7660cc45..a9d609e7 100644
--- a/src/NumSharp.Core/Operations/Elementwise/NdArray.Division.cs
+++ b/src/NumSharp.Core/Operations/Elementwise/NdArray.Division.cs
@@ -1,150 +1,231 @@
-using System;
+/*
+This file was generated by template ../NDArray.Elementwise.tt
+In case you want to do some changes do the following
+
+1 ) adapt the tt file
+2 ) execute powershell file "GenerateCode.ps1" on root level
+
+*/
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Numerics;
-
-
+using NumSharp.Core.Shared;
+
namespace NumSharp.Core
{
public partial class NDArray
{
public static NDArray operator /(NDArray np1, NDArray np2)
- {
- int scalarNo = -1;
+ {
+ /// following code is for determine if scalar or not
+ /// also for determine result
+ int scalarNo = !(np1.ndim == 0 || np2.ndim == 0) ? 0 : -1;
- var sum = np1.DetermineEmptyResult(np2,ref scalarNo);
-
- Array np1SysArr = np1.Storage.GetData();
- Array np2SysArr = np2.Storage.GetData();
- Array np3SysArr = sum.Storage.GetData();
-
- switch (np3SysArr)
+ if( scalarNo == 0 )
{
- case double[] sumArray :
+ if (!Enumerable.SequenceEqual(np1.shape,np2.shape))
{
- double[] np1Array = np1SysArr as double[];
- double[] np2Array = np2SysArr as double[];
-
- double scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
- break;
+ throw new IncorrectShapeException();
}
- case float[] sumArray :
- {
- float[] np1Array = np1SysArr as float[];
- float[] np2Array = np2SysArr as float[];
-
- float scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
- break;
- }
- case int[] sumArray :
+ }
+ else
+ {
+ if (np1.ndim == 0)
+ scalarNo = 1;
+ else
+ scalarNo = 2;
+ }
+
+ NDArray result = null;
+
+ switch (scalarNo)
+ {
+ case 1 :
{
- int[] np1Array = np1SysArr as int[];
- int[] np2Array = np2SysArr as int[];
-
- int scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
+ result = new NDArray(np2.dtype,np2.shape);
break;
}
- case Int64[] sumArray :
+ case 2 :
{
- Int64[] np1Array = np1SysArr as Int64[];
- Int64[] np2Array = np2SysArr as Int64[];
-
- Int64 scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
+ result = new NDArray(np1.dtype,np1.shape);
break;
}
- case Complex[] sumArray :
+ default :
{
- Complex[] np1Array = np1SysArr as Complex[];
- Complex[] np2Array = np2SysArr as Complex[];
-
- Complex scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
+ result = new NDArray(np1.dtype,np1.shape);
break;
- }
- case Quaternion[] sumArray :
- {
- Quaternion[] np1Array = np1SysArr as Quaternion[];
- Quaternion[] np2Array = np2SysArr as Quaternion[];
-
- Quaternion scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np2Array[idx] / scalar;
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] / scalar;
- break;
- }
+ }
+ }
+
+ Array np1SysArr = np1.Storage.GetData();
+ Array np2SysArr = np2.Storage.GetData();
+ Array np3SysArr = result.Storage.GetData();
+
+ switch (np3SysArr)
+ {
+
+ case System.Int32[] resArr :
+ {
+ System.Int32[] np1Array = np1SysArr as System.Int32[];
+ System.Int32[] np2Array = np2SysArr as System.Int32[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int32 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int32 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
+
+ case System.Int64[] resArr :
+ {
+ System.Int64[] np1Array = np1SysArr as System.Int64[];
+ System.Int64[] np2Array = np2SysArr as System.Int64[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int64 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int64 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
+
+ case System.Single[] resArr :
+ {
+ System.Single[] np1Array = np1SysArr as System.Single[];
+ System.Single[] np2Array = np2SysArr as System.Single[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Single scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Single scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
+
+ case System.Double[] resArr :
+ {
+ System.Double[] np1Array = np1SysArr as System.Double[];
+ System.Double[] np2Array = np2SysArr as System.Double[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Double scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Double scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Complex[] resArr :
+ {
+ System.Numerics.Complex[] np1Array = np1SysArr as System.Numerics.Complex[];
+ System.Numerics.Complex[] np2Array = np2SysArr as System.Numerics.Complex[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Complex scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Complex scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Quaternion[] resArr :
+ {
+ System.Numerics.Quaternion[] np1Array = np1SysArr as System.Numerics.Quaternion[];
+ System.Numerics.Quaternion[] np2Array = np2SysArr as System.Numerics.Quaternion[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Quaternion scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar / np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Quaternion scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] / scalar;
+ }
+ break;
+ }
default :
{
throw new IncorrectTypeException();
}
}
- return sum;
+ return result;
}
}
}
+
diff --git a/src/NumSharp.Core/Operations/Elementwise/NdArray.Multiplication.cs b/src/NumSharp.Core/Operations/Elementwise/NdArray.Multiplication.cs
index 7b08e63c..ad2aed91 100644
--- a/src/NumSharp.Core/Operations/Elementwise/NdArray.Multiplication.cs
+++ b/src/NumSharp.Core/Operations/Elementwise/NdArray.Multiplication.cs
@@ -1,4 +1,12 @@
-using System;
+/*
+This file was generated by template ../NDArray.Elementwise.tt
+In case you want to do some changes do the following
+
+1 ) adapt the tt file
+2 ) execute powershell file "GenerateCode.ps1" on root level
+
+*/
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
@@ -11,140 +19,213 @@ namespace NumSharp.Core
public partial class NDArray
{
public static NDArray operator *(NDArray np1, NDArray np2)
- {
- int scalarNo = -1;
-
- var sum = np1.DetermineEmptyResult(np2,ref scalarNo);
+ {
+ /// following code is for determine if scalar or not
+ /// also for determine result
+ int scalarNo = !(np1.ndim == 0 || np2.ndim == 0) ? 0 : -1;
- Array np1SysArr = np1.Storage.GetData();
- Array np2SysArr = np2.Storage.GetData();
- Array np3SysArr = sum.Storage.GetData();
-
- switch (np3SysArr)
+ if( scalarNo == 0 )
{
- case double[] sumArray :
- {
- double[] np1Array = np1SysArr as double[];
- double[] np2Array = np2SysArr as double[];
-
- double scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
- break;
- }
- case float[] sumArray :
+ if (!Enumerable.SequenceEqual(np1.shape,np2.shape))
{
- float[] np1Array = np1SysArr as float[];
- float[] np2Array = np2SysArr as float[];
-
- float scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
- break;
+ throw new IncorrectShapeException();
}
- case int[] sumArray :
+ }
+ else
+ {
+ if (np1.ndim == 0)
+ scalarNo = 1;
+ else
+ scalarNo = 2;
+ }
+
+ NDArray result = null;
+
+ switch (scalarNo)
+ {
+ case 1 :
{
- int[] np1Array = np1SysArr as int[];
- int[] np2Array = np2SysArr as int[];
-
- int scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
+ result = new NDArray(np2.dtype,np2.shape);
break;
}
- case Int64[] sumArray :
+ case 2 :
{
- Int64[] np1Array = np1SysArr as Int64[];
- Int64[] np2Array = np2SysArr as Int64[];
-
- Int64 scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
+ result = new NDArray(np1.dtype,np1.shape);
break;
}
- case Complex[] sumArray :
+ default :
{
- Complex[] np1Array = np1SysArr as Complex[];
- Complex[] np2Array = np2SysArr as Complex[];
-
- Complex scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
+ result = new NDArray(np1.dtype,np1.shape);
break;
- }
- case Quaternion[] sumArray :
- {
- Quaternion[] np1Array = np1SysArr as Quaternion[];
- Quaternion[] np2Array = np2SysArr as Quaternion[];
-
- Quaternion scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] * np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar * np1Array[idx];
- break;
- }
+ }
+ }
+
+ Array np1SysArr = np1.Storage.GetData();
+ Array np2SysArr = np2.Storage.GetData();
+ Array np3SysArr = result.Storage.GetData();
+
+ switch (np3SysArr)
+ {
+
+ case System.Int32[] resArr :
+ {
+ System.Int32[] np1Array = np1SysArr as System.Int32[];
+ System.Int32[] np2Array = np2SysArr as System.Int32[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int32 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int32 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
+
+ case System.Int64[] resArr :
+ {
+ System.Int64[] np1Array = np1SysArr as System.Int64[];
+ System.Int64[] np2Array = np2SysArr as System.Int64[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int64 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int64 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
+
+ case System.Single[] resArr :
+ {
+ System.Single[] np1Array = np1SysArr as System.Single[];
+ System.Single[] np2Array = np2SysArr as System.Single[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Single scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Single scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
+
+ case System.Double[] resArr :
+ {
+ System.Double[] np1Array = np1SysArr as System.Double[];
+ System.Double[] np2Array = np2SysArr as System.Double[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Double scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Double scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Complex[] resArr :
+ {
+ System.Numerics.Complex[] np1Array = np1SysArr as System.Numerics.Complex[];
+ System.Numerics.Complex[] np2Array = np2SysArr as System.Numerics.Complex[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Complex scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Complex scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Quaternion[] resArr :
+ {
+ System.Numerics.Quaternion[] np1Array = np1SysArr as System.Numerics.Quaternion[];
+ System.Numerics.Quaternion[] np2Array = np2SysArr as System.Numerics.Quaternion[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Quaternion scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar * np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Quaternion scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] * scalar;
+ }
+ break;
+ }
default :
{
throw new IncorrectTypeException();
}
}
- return sum;
+ return result;
}
}
}
+
diff --git a/src/NumSharp.Core/Operations/Elementwise/NdArray.Substraction.cs b/src/NumSharp.Core/Operations/Elementwise/NdArray.Substraction.cs
index 7425f35c..a866319a 100644
--- a/src/NumSharp.Core/Operations/Elementwise/NdArray.Substraction.cs
+++ b/src/NumSharp.Core/Operations/Elementwise/NdArray.Substraction.cs
@@ -1,150 +1,231 @@
-using System;
+/*
+This file was generated by template ../NDArray.Elementwise.tt
+In case you want to do some changes do the following
+
+1 ) adapt the tt file
+2 ) execute powershell file "GenerateCode.ps1" on root level
+
+*/
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Numerics;
-
-
+using NumSharp.Core.Shared;
+
namespace NumSharp.Core
{
public partial class NDArray
{
public static NDArray operator -(NDArray np1, NDArray np2)
- {
- int scalarNo = -1;
+ {
+ /// following code is for determine if scalar or not
+ /// also for determine result
+ int scalarNo = !(np1.ndim == 0 || np2.ndim == 0) ? 0 : -1;
- var sum = np1.DetermineEmptyResult(np2,ref scalarNo);
-
- Array np1SysArr = np1.Storage.GetData();
- Array np2SysArr = np2.Storage.GetData();
- Array np3SysArr = sum.Storage.GetData();
-
- switch (np3SysArr)
+ if( scalarNo == 0 )
{
- case double[] sumArray :
+ if (!Enumerable.SequenceEqual(np1.shape,np2.shape))
{
- double[] np1Array = np1SysArr as double[];
- double[] np2Array = np2SysArr as double[];
-
- double scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
- break;
+ throw new IncorrectShapeException();
}
- case float[] sumArray :
- {
- float[] np1Array = np1SysArr as float[];
- float[] np2Array = np2SysArr as float[];
-
- float scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
- break;
- }
- case int[] sumArray :
+ }
+ else
+ {
+ if (np1.ndim == 0)
+ scalarNo = 1;
+ else
+ scalarNo = 2;
+ }
+
+ NDArray result = null;
+
+ switch (scalarNo)
+ {
+ case 1 :
{
- int[] np1Array = np1SysArr as int[];
- int[] np2Array = np2SysArr as int[];
-
- int scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
+ result = new NDArray(np2.dtype,np2.shape);
break;
}
- case Int64[] sumArray :
+ case 2 :
{
- Int64[] np1Array = np1SysArr as Int64[];
- Int64[] np2Array = np2SysArr as Int64[];
-
- Int64 scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
+ result = new NDArray(np1.dtype,np1.shape);
break;
}
- case Complex[] sumArray :
+ default :
{
- Complex[] np1Array = np1SysArr as Complex[];
- Complex[] np2Array = np2SysArr as Complex[];
-
- Complex scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
+ result = new NDArray(np1.dtype,np1.shape);
break;
- }
- case Quaternion[] sumArray :
- {
- Quaternion[] np1Array = np1SysArr as Quaternion[];
- Quaternion[] np2Array = np2SysArr as Quaternion[];
-
- Quaternion scalar = ( scalarNo == 1 ) ? np1.Storage.GetData()[0] : np2.Storage.GetData()[0];
-
- // for is faster than linq
- if( scalarNo == 0 )
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - np2Array[idx];
- else if (scalarNo == 1)
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = scalar - np2Array[idx];
- else
- for (int idx = 0; idx < sumArray.Length;idx++)
- sumArray[idx] = np1Array[idx] - scalar;
- break;
- }
+ }
+ }
+
+ Array np1SysArr = np1.Storage.GetData();
+ Array np2SysArr = np2.Storage.GetData();
+ Array np3SysArr = result.Storage.GetData();
+
+ switch (np3SysArr)
+ {
+
+ case System.Int32[] resArr :
+ {
+ System.Int32[] np1Array = np1SysArr as System.Int32[];
+ System.Int32[] np2Array = np2SysArr as System.Int32[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int32 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int32 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
+
+ case System.Int64[] resArr :
+ {
+ System.Int64[] np1Array = np1SysArr as System.Int64[];
+ System.Int64[] np2Array = np2SysArr as System.Int64[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Int64 scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Int64 scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
+
+ case System.Single[] resArr :
+ {
+ System.Single[] np1Array = np1SysArr as System.Single[];
+ System.Single[] np2Array = np2SysArr as System.Single[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Single scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Single scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
+
+ case System.Double[] resArr :
+ {
+ System.Double[] np1Array = np1SysArr as System.Double[];
+ System.Double[] np2Array = np2SysArr as System.Double[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Double scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Double scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Complex[] resArr :
+ {
+ System.Numerics.Complex[] np1Array = np1SysArr as System.Numerics.Complex[];
+ System.Numerics.Complex[] np2Array = np2SysArr as System.Numerics.Complex[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Complex scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Complex scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
+
+ case System.Numerics.Quaternion[] resArr :
+ {
+ System.Numerics.Quaternion[] np1Array = np1SysArr as System.Numerics.Quaternion[];
+ System.Numerics.Quaternion[] np2Array = np2SysArr as System.Numerics.Quaternion[];
+ np1Array = (np1Array == null) ? np1.Storage.CloneData() : np1Array;
+ np2Array = (np2Array == null) ? np2.Storage.CloneData() : np2Array;
+
+ if (scalarNo == 0 )
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - np2Array[idx];
+ else if (scalarNo == 1 )
+ {
+ System.Numerics.Quaternion scalar = np1.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = scalar - np2Array[idx];
+ }
+ else if (scalarNo == 2 )
+ {
+ System.Numerics.Quaternion scalar = np2.Storage.CloneData()[0];
+ for( int idx = 0; idx < np3SysArr.Length;idx++)
+ resArr[idx] = np1Array[idx] - scalar;
+ }
+ break;
+ }
default :
{
throw new IncorrectTypeException();
}
}
- return sum;
+ return result;
}
}
}
+
diff --git a/src/NumSharp.Core/Operations/NdArray.ElementsWise.tt b/src/NumSharp.Core/Operations/NdArray.ElementsWise.tt
new file mode 100644
index 00000000..268417f0
--- /dev/null
+++ b/src/NumSharp.Core/Operations/NdArray.ElementsWise.tt
@@ -0,0 +1,161 @@
+<#@ template language="C#" #>
+<#@ parameter type="System.String" name="operationName" #>
+<#@ parameter type="System.String" name="operationTypesString" #>
+<#@ parameter type="System.String" name="operationExpressionString" #>
+<#
+ System.String[] operationTypes = operationTypesString.Split(';');
+ System.String[] converter = new System.String[operationTypes.Length];
+
+ for (int idx = 0;idx < converter.Length;idx++)
+ {
+ //operationTypes[idx] = System.Type.GetType(operationTypes[idx]).FullName;
+ switch(operationTypes[idx])
+ {
+ case "System.Int64" :
+ {
+ converter[idx] = "System.Convert.ToInt64";
+ break;
+ }
+ case "System.Int32" :
+ {
+ converter[idx] = "System.Convert.ToInt32";
+ break;
+ }
+ case "System.Int16" :
+ {
+ converter[idx] = "System.Convert.ToInt16";
+ break;
+ }
+ case "System.Double" :
+ {
+ converter[idx] = "System.Convert.ToDouble";
+ break;
+ }
+ case "System.Single" :
+ {
+ converter[idx] = "System.Convert.ToSingle";
+ break;
+ }
+ case "System.Numerics.Complex" :
+ {
+ converter[idx] = "NumSharp.Core.Converter.ToComplex";
+ break;
+ }
+ case "System.Numerics.Quaternion" :
+ {
+ converter[idx] = "NumSharp.Core.Converter.ToQuaternion";
+ break;
+ }
+ }
+ }
+#>
+/*
+This file was generated by template ../NDArray.Elementwise.tt
+In case you want to do some changes do the following
+
+1 ) adapt the tt file
+2 ) execute powershell file "GenerateCode.ps1" on root level
+
+*/
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Numerics;
+using NumSharp.Core.Shared;
+
+namespace NumSharp.Core
+{
+ public partial class NDArray
+ {
+ public static NDArray operator <#=operationName#>(NDArray np1, NDArray np2)
+ {
+ /// following code is for determine if scalar or not
+ /// also for determine result
+ int scalarNo = !(np1.ndim == 0 || np2.ndim == 0) ? 0 : -1;
+
+ if( scalarNo == 0 )
+ {
+ if (!Enumerable.SequenceEqual(np1.shape,np2.shape))
+ {
+ throw new IncorrectShapeException();
+ }
+ }
+ else
+ {
+ if (np1.ndim == 0)
+ scalarNo = 1;
+ else
+ scalarNo = 2;
+ }
+
+ NDArray result = null;
+
+ switch (scalarNo)
+ {
+ case 1 :
+ {
+ result = new NDArray(np2.dtype,np2.shape);
+ break;
+ }
+ case 2 :
+ {
+ result = new NDArray(np1.dtype,np1.shape);
+ break;
+ }
+ default :
+ {
+ result = new NDArray(np1.dtype,np1.shape);
+ break;
+ }
+ }
+
+ Array np1SysArr = np1.Storage.GetData();
+ Array np2SysArr = np2.Storage.GetData();
+ Array np3SysArr = result.Storage.GetData();
+
+ switch (np3SysArr)
+ {
+ <#
+ for (int idx = 0; idx < operationTypes.Length;idx++)
+ {
+ WriteLine("");
+ WriteLine("\t\t\t\tcase " + operationTypes[idx] + "[] resArr : ");
+ WriteLine("\t\t\t\t{");
+ WriteLine("\t\t\t\t " + operationTypes[idx] + "[] np1Array = np1SysArr as " + operationTypes[idx] + "[];");
+ WriteLine("\t\t\t\t " + operationTypes[idx] + "[] np2Array = np2SysArr as " + operationTypes[idx] + "[];");
+ WriteLine("\t\t\t\t np1Array = (np1Array == null) ? np1.Storage.CloneData<" + operationTypes[idx] + ">() : np1Array;");
+ WriteLine("\t\t\t\t np2Array = (np2Array == null) ? np2.Storage.CloneData<" + operationTypes[idx] + ">() : np2Array;");
+ WriteLine("");
+ WriteLine("\t\t\t\t if (scalarNo == 0 )");
+ WriteLine("\t\t\t\t for( int idx = 0; idx < np3SysArr.Length;idx++)");
+ WriteLine("\t\t\t\t resArr[idx] = np1Array[idx] " + operationName + " np2Array[idx];");
+ WriteLine("\t\t\t\t else if (scalarNo == 1 )");
+ WriteLine("\t\t\t\t {");
+ WriteLine("\t\t\t\t " + operationTypes[idx] + " scalar = np1.Storage.CloneData<" + operationTypes[idx] + ">()[0];" );
+ WriteLine("\t\t\t\t for( int idx = 0; idx < np3SysArr.Length;idx++)");
+ WriteLine("\t\t\t\t resArr[idx] = scalar " + operationName + " np2Array[idx];");
+ WriteLine("\t\t\t\t }");
+ WriteLine("\t\t\t\t else if (scalarNo == 2 )");
+ WriteLine("\t\t\t\t {");
+ WriteLine("\t\t\t\t " + operationTypes[idx] + " scalar = np2.Storage.CloneData<" + operationTypes[idx] + ">()[0];" );
+ WriteLine("\t\t\t\t for( int idx = 0; idx < np3SysArr.Length;idx++)");
+ WriteLine("\t\t\t\t resArr[idx] = np1Array[idx] " + operationName + " scalar;");
+ WriteLine("\t\t\t\t }");
+ WriteLine("\t\t\t\t break;");
+ WriteLine("\t\t\t\t}");
+ }
+ #>
+ default :
+ {
+ throw new IncorrectTypeException();
+ }
+ }
+
+ return result;
+ }
+
+ }
+
+}
diff --git a/test/NumSharp.UnitTest/NdArray.Test.cs b/test/NumSharp.UnitTest/NdArray.Test.cs
index 6b2a8cdf..9247b78d 100644
--- a/test/NumSharp.UnitTest/NdArray.Test.cs
+++ b/test/NumSharp.UnitTest/NdArray.Test.cs
@@ -14,26 +14,26 @@ public class NDArrayTest
[TestMethod]
public void StringCheck()
{
- /*
- var nd = np.arange(9.0).reshape(3,3);
+
+ var nd = np.arange(9.0).reshape(3,3).MakeGeneric();
var random = new Random();
- nd.Set(nd.Data().Select(x => x + random.NextDouble()).ToArray());
+ nd.Storage.SetData(nd.Data().Select(x => x + random.NextDouble()).ToArray());
nd[1,0] = 1.0;
nd[0,0] = 9.0;
nd[2,2] = 7.0;
- //nd.Storage[3] -= 20;
- //nd.Storage[8] += 23;
+ nd[0,2] = nd[0,2] - 20.0;
+ nd[2,2] += 23;
var stringOfNp = nd.ToString();
- /*Assert.IsTrue(stringOfNp.Contains("[[ 0."));
+ Assert.IsTrue(stringOfNp.Contains("[["));
- nd = np.arange(9).reshape(3,3);
+ nd = np.arange(9).reshape(3,3).MakeGeneric();
stringOfNp = nd.ToString();
- Assert.IsTrue(stringOfNp.Contains("([[ 0,"));*/
+ Assert.IsTrue(stringOfNp.Contains("([[ 0,"));
}
[TestMethod]