0

ベクトルと行列に対して異なる方法で機能する関数を Sage で作成しています。

isinstanceベクトルまたは行列のタイプは要素のタイプに依存するため、関数を使用できません。

sage: type(matrix([[1]]))
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
sage: type(matrix([[i]]))
<type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>

ベクトルと行列を区別する最良の方法は何ですか?

4

1 に答える 1

1

matrix.dimSage ソースで定義を見つけようとしているときに、ソリューションが誤って見つかりました。

from sage.matrix.matrix import is_Matrix
from sage.structure.element import is_Vector

def myfunction(x):
    if is_Vector(x):
        # do something
    elif is_Matrix(x):
        # do something else
    else:
        raise TypeError("The argument must be vector or matrix")
于 2012-01-05T13:22:25.967 に答える