# 请问如何用矩阵左乘向量

## 运行环境

``````[Taichi] version 0.8.3, llvm 10.0.0, commit 021af5d2, osx, python 3.9.7
[Taichi] Starting on arch=arm64
``````

## 问题描述

``````        X = ti.Vector([x1, x2, x3, x4])
Y = ti.Vector([y1, y2, y3, y4]).transpose()
_J = ti.Matrix([[0, 1 - t, t - s, s - 1],
[t - 1, 0, s + 1, -s - t],
[s - t, -s - 1, 0, t + 1],
[1 - s, s + t, -t - 1, 0]])
# J = X.dot(_J).dot(Y) / 8. # AssertionError: rhs for dot is not a vector
J = X @ _J @ Y / 8. # AssertionError: Dimension mismatch between shapes (4, 1), (4, 4)
``````

`J = _J @ X @ Y / 8.` 应该可以？

Hi, @AlbertLiDesign, 针对你的问题，我写了一个小的例子，不知道能解决你的问题不？

``````import taichi as ti
ti.init(arch=ti.cpu)

@ti.kernel
def test():
x = ti.Vector([1,2,3,4])
y = x.transpose()
A = ti.Matrix([[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]])
c = y @ A @ x
print(c)

test() # output: [30]
``````

``````import taichi as ti
ti.init()
@ti.kernel
def test():
X = ti.Vector([1, 2, 3, 4])  # column vector
Y = ti.Vector([1, 2, 3, 4]).transpose() # row vector
J = ti.Matrix([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
ret = Y @ J  # row vector left mul
ret2 = J @ X # column vector right mul
print(ret, ret2) # ret: [[10, 20, 30, 40]] ret2: [30, 30, 30, 30]
test()
``````