Skip to content

Commit b65e13d

Browse files
committed
feat(csharp/Tensor.NET): add foreach methods for tensor.
1 parent cc25f02 commit b65e13d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

csharp/Tensor.NET/Tensor/Common/InplaceOperation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,17 @@ public static void Execute<T>(Tensor<T> inp, Tensor<bool> condition, Tensor<T> r
7878
}
7979
}
8080
}
81+
}
82+
83+
namespace Tensornet{
84+
public partial class Tensor<T>{
85+
/// <summary>
86+
/// Apply an function on each element of the current tensor inplace.
87+
/// Note that the modification will be made on the current tensor directly.
88+
/// </summary>
89+
/// <param name="operation"></param>
90+
public void ForEachInplace(Func<T, T> operation){
91+
Common.InplaceOperation.Execute(this, operation);
92+
}
93+
}
8194
}

csharp/Tensor.NET/Tensor/Common/OnElemOperation.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ public static Tensor<TResult> Execute<TInput, TResult>(Tensor<TInput> inp, Func<
2828
return res;
2929
}
3030
}
31+
}
32+
33+
namespace Tensornet{
34+
public partial class Tensor<T>{
35+
/// <summary>
36+
/// Apply a function to each element of the current tensor.
37+
/// Note that a new tensor will be created. If you want to directly modify the tensor, please use "ForEachInplace"
38+
/// </summary>
39+
/// <typeparam name="TResult"></typeparam>
40+
/// <param name="operation"></param>
41+
/// <returns></returns>
42+
public Tensor<TResult> ForEach<TResult>(Func<T, TResult> operation) where TResult : struct, IEquatable<TResult>, IConvertible{
43+
return Common.OnElemOperation.Execute(this, operation);
44+
}
45+
}
3146
}

doc/TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
| Add zeros_like, ones_like, fill_like methods | | p0 | Complete ✅ |
5656
| Add flatten method | | p0 | Complete ✅ |
5757
| Add max, min, mean op | | p0 | Complete ✅ |
58+
| Add mod, and, or, xor operators in csharp | | p0 | Waiting 🔵 |
5859

5960

6061
✅ ❌ 🚀 🔵

0 commit comments

Comments
 (0)