I want to display a matrix with putting an extracted common factor on outside of the matrix after matrix calculation in sympy.

我希望在sympy中进行矩阵计算后显示一个矩阵,将提取的公因子放在矩阵外部。

I wrote below code.

我在下面写了代码。

from sympy import *
a = symbols("a")
b = symbols("b")

A = Matrix([exp(I*a),exp(I*a)*exp(I*b)])
print simplify(A)

I got below output.

我得到了以下输出。

Matrix([
[      exp(I*a)],
[exp(I*(a + b))]])

However, I want to get below output.

但是,我想要低于输出。

exp(I*a)*Matrix([
[      1],
[exp(I*b)]])

I tried collect(A,exp(I*a)) and got follow error.

我试过收集(A,exp(I * a))并得到了跟随错误。

Traceback (most recent call last):

  File "<ipython-input-65-834f4c326df4>", line 1, in <module>
    runfile('C:/Anaconda2/Programs/test/untitled44.py', wdir='C:/Anaconda2/Programs/test')

  File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Anaconda2/Programs/test/untitled44.py", line 14, in <module>
    collect(A,exp(I*a))

  File "C:\Anaconda2\lib\site-packages\sympy\simplify\simplify.py", line 451, in collect
    if expr.is_Mul:

  File "C:\Anaconda2\lib\site-packages\sympy\matrices\matrices.py", line 3084, in __getattr__
    "%s has no attribute %s." % (self.__class__.__name__, attr))

AttributeError: MutableDenseMatrix has no attribute is_Mul.

I know a way to extract a common factor for a element of a matrix like follow link. https://github.com/sympy/sympy/issues/8442

我知道一种方法来提取像跟随链接这样的矩阵元素的公因子。 https://github.com/sympy/sympy/issues/8442

But it's not my desire.

但这不是我的愿望。

How should I do?

我应该怎么做?

1 个解决方案

#1


2

I do not think Sympy provides a function for the task you want. However, you can do this manually, as per the method proposed in the accepted answer of a similar question asked in the Mathematica SE (link).

我不认为Sympy为您想要的任务提供功能。但是,您可以按照Mathematica SE(链接)中提出的类似问题的接受答案中提出的方法手动执行此操作。

The idea is to extract the common factor of the polynomial elements via gcd and then use MatMul with the evaluate=False option in order to restrict Sympy from performing the scalar-matrix multiplication.

想法是通过gcd提取多项式元素的公因子,然后使用MatMul和evaluate = False选项,以限制Sympy执行标量矩阵乘法。

import sympy as sp

a, b = sp.symbols('a, b')

A = sp.Matrix([sp.exp(sp.I * a), sp.exp(sp.I * a) * sp.exp(sp.I * b)])
g = sp.gcd(tuple(A))

A_v2 = sp.MatMul(g,(A/g),evaluate = False)
print(A_v2)
exp(I*a)*Matrix([
[       1],
[exp(I*b)]])

更多相关文章

  1. leet240. 搜索二维矩阵 II
  2. java小练习(一个数如果恰好等于它的因子之和,这个数就称为“完数”
  3. 是否有缩放因子方法用于Android的多分辨率支持

随机推荐

  1. 利用Intellij+MAVEN搭建Spring+Mybatis+M
  2. MySQL、JDBC安装以及用java连接
  3. mySql中SUBSTRING_INDEX函数用法
  4. mysql数据库之单表查询
  5. MYSQL如何导出存储过程和触发器?
  6. mysql百万级查询怎么样才能快速查询呢
  7. MySQL5.7 GTID学习笔记
  8. 如何更改select语句中的列值
  9. shell脚本中mysqldump的基本使用
  10. Navicat连接mysql8出现1251错误