Python到C#多维数组

2024-05-14 23:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将这段代码从python翻译成c#:

def _get_w_matrix(quaternions):
    w_matrix = [[[q[3], q[2], -q[1], q[0]],
                 [-q[2], q[3] , q[0], q[1]],
                 [q[1], -q[0], q[3], q[2]],
                 [-q[0], -q[1], -q[2], q[3]]] for q in quaternions] 

其中,四元数类似于

[[0. 0. 0. 0.]
 [0. 2. 2. 0.]
 [2. 3. 1. 0.]]

我不明白返回的是哪种类型的对象以及如何写下它

编辑: 我将Numpy用于.NET,因此在我的c#代码中,变量四元数的类型是NDarray


Tags: 对象代码innumpy编辑类型forget
1条回答
网友
1楼 · 发布于 2024-05-14 23:37:02

Pythontype()函数和print()函数是两个可用于调试w_matrix list的外观和类型的函数

我编写此代码是为了调试:

def _get_w_matrix(quaternions):
    w_matrix = [[[q[3], q[2], -q[1], q[0]],
                 [-q[2], q[3] , q[0], q[1]],
                 [q[1], -q[0], q[3], q[2]],
                 [-q[0], -q[1], -q[2], q[3]]] for q in quaternions] 
    return w_matrix

inputMatrix = [[0,0,0,0],
 [0,2,2,0],
 [2,3,1,0]]

test = _get_w_matrix(inputMatrix)

print("matrix created by function: ")
print(test)
print("type of matrix:")
print(type(test))

返回:

matrix created by function: 
[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 2, -2, 0], [-2, 0, 0, 2], [2, 0, 0, 2], [0, -2, -2, 0]], [[0, 1, -3, 2], [-1, 0, 2, 3], [3, -2, 0, 1], [-2, -3, -1, 0]]]
type of matrix:
<class 'list'>

该类型是一个包含整数的python列表。我不是c#方面的专家,但这应该与列表之类的东西相对应<;列表<;列表>&燃气轮机&燃气轮机

相关问题 更多 >

    热门问题