Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lang] Support passing numpy arrays to ti.Matrix constructor #1044

Merged
merged 1 commit into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/cornell_box.py
Expand Up @@ -62,8 +62,6 @@ def make_box_transform_matrices():
m = translate @ rot
m_inv = np.linalg.inv(m)
m_inv_t = np.transpose(m_inv)
m_inv = [list(r) for r in m_inv]
m_inv_t = [list(r) for r in m_inv_t]
return ti.Matrix(m_inv), ti.Matrix(m_inv_t)


Expand Down
15 changes: 8 additions & 7 deletions python/taichi/lang/matrix.py
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from .util import to_numpy_type, to_pytorch_type
from .common_ops import TaichiOperations
from collections import Iterable


def broadcast_if_scalar(func):
Expand Down Expand Up @@ -60,10 +61,13 @@ def __init__(self,
self.n = t.n
self.m = t.m
self.entries = t.entries
elif isinstance(n, list):
if n == []:
elif isinstance(n, list) or isinstance(n, np.ndarray):
if len(n) == 0:
mat = []
elif not isinstance(n[0], list):
elif isinstance(n[0], Matrix):
raise Exception(
'cols/rows required when using list of vectors')
elif not isinstance(n[0], Iterable):
if impl.get_runtime().inside_kernel:
# wrap potential constants with Expr
if keep_raw:
Expand All @@ -72,11 +76,8 @@ def __init__(self,
mat = [list([expr.Expr(x)]) for x in n]
else:
mat = [[x] for x in n]
elif isinstance(n[0], Matrix):
raise Exception(
'cols/rows required when using list of vectors')
else:
mat = n
mat = [list(r) for r in n]
self.n = len(mat)
if len(mat) > 0:
self.m = len(mat[0])
Expand Down