打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Casting Arrays – ILNumerics – Math Library for C# and .NET
Creating ILArray<T> by Casting
Implicit and explicit casting operators exist for the conversion of ILNumerics arrays to system types.
Scalars
A singleton System type T can always be converted to a scalar ILNumerics array of matching element type - implicitly. Therefore, system types can directly get assigned to array variables and given as parameter for functions, expecting an ILNumerics array:
C#-Code
ILArray<double> A = 1.0;
1
ILArray<double> A = 1.0;
2
ILArray<double> B = 2.0f;
3
B = sin(sqrt(2.0));
The other way around must be explicitly casted, since arrays are not always scalar:
C#-Code
double a = (double) A; // works, if A is scalar1
1
double a = (double) A; // works, if A is scalar
An exception will be thrown when attempting to cast a nonscalar ILArray to a single system type element.
Vectors
One dimensional System.Array T[] convert implicitly to ILNumerics arrays. This will result in a column vector:
C#-Code
ILArray<double> A = new double[] {1.0, 2.0, 3.0}; 5
1
ILArray<double> A = new double[] {1.0, 2.0, 3.0};
2
//<Double> [3,1]
3
//  1
4
//  2
5
//  3
The cast uses the original System.Array as internal memory for the resulting array. The reverse direction is not supported. SeeImporting and Exporting Elements from Arrays for options on system type interaction.
Multidimensional Case
Multidimensional System.Array implicitly cast to ILNumerics arrays of the same rank, shape and element type:
C#-Code
ILArray<double> A = new double[,]{{1.0,2.0,3.0},{5.0,6.0,7.0}};5
1
ILArray<double> A = new double[,]{{1.0,2.0,3.0},{5.0,6.0,7.0}};
2
//<Double> [3,2]
3
//  1          5
4
//  2          6
5
//  3          7
The inner-most dimension of the source corresponds to the columns of the resulting array. Therefore, when converting a matrix from a System.Array that way one will observe a transposed matrix in the resulting array! Or more general: the resulting array will expose the same element ordering in the internal storage as the source but its dimensions will be flipped.
The conversion from System.Array to ILArray<T> is the most general case supported by ILNumerics. It includes the conversion from arbitrary System.Arrays, including object[] and object[,] ... However, the elements stored in the source array must be either of:
* Scalar System.Value types: double, float, int, uint, long, ulong, short, ushort, byte, sbyte.
* Scalar ILArray<T> type, where T is one of the above System.Value types.
C#-Code Example
ILArray<double> A1 = new[] { 1, ILMath.cos(2.0), 3, 4 };
1
ILArray<double> A1 = new[] { 1, ILMath.cos(2.0), 3, 4 };
2
// convert from mixed types
3
double B = -1, C = 10;
4
ILArray<double> A2 = new[] { ILMath.cos(A1[2]), ILMath.tan(B) * C, C, 3 };
5
// create from multidimensional System.Array
6
ILArray<int> A3 = new[,] { { 11, 12, 13 }, { 21, 22, 23 } };
7
// narrowing conversion: from double to int
8
ILArray<int> A4 = new[,] { { 11.9, 12.1, 13 }, { 21.5, 22.5, 23 } };
9
// Note the rounding rules!
10
//<Int32> [3,2]
11
// 12 22
12
// 12 22
13
// 13 23
Creating arrays this way involves a shallow copy of the source elements into new memory. The original System.Array will not get referenced afterwards from ILNumerics.
ILNumerics arrays created from empty System.Array by implicit casts will retain the shape of the source.
A Note about .NET Jagged Arrays
ILNumerics supports the concept of rectilinear arrays. In this context, that means along each specified (arbitrary) dimensions the number of the elements must be the same. Since .NET jagged arrays does not ensuring that, that is why ILNumerics does not support the implicit conversion from such arrays.
Note about memory ownership
ILNumerics is greedy in taking ownership of any source array provided. However, this works out for single-dimensional T[], where T is a simple, numeric primitive value type only.
Example: assigning a System.Array of element type double (Double in Visual Basic) to a local variable of type ILArray<double>.
C#-Code
double[] array = new double[] {1.0, 2.0, 3.0, 4.0};
1
double[] array = new double[] {1.0, 2.0, 3.0, 4.0};
2
ILArray<double> A = array; // this gives ownership of array to the ILNumerics memory pool!
3
4
// make sure not to reference 'array' directly anymore
Example: providing System.Array as argument to a function which expects an parameter of type ILInArray<T>.
C#-Code
double[] array = new double[] {1.0, 2.0, 3.0, 4.0};
1
double[] array = new double[] {1.0, 2.0, 3.0, 4.0};
2
3
// using array as argument will implicitly cast it to ILInArray<double>
4
DoSomething(array); // <- this gives ownership of array to the ILNumerics memory pool!
5
// make sure not to reference 'array' directly anymore
6
7
void DoSomething(ILInArray<double> Arg1) {
8
using (ILScope.Enter(Arg1) {
9
// do something...
10
}
11
}
12
See also:Implicit conversion operator in the class reference
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Bytecode for the Dalvik VM | Android Developers
欧美式棒针符号
2021
php所有报错列表—php所有报错号码的意义—PHP错误解析器代号 | 一人*一言堂
OpenCV学习笔记(矩阵和图象操作)
C -入门语法(五)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服