向量对矩阵乘积的热求和

编程入门 行业动态 更新时间:2024-10-26 18:28:34
本文介绍了向量对矩阵乘积的热求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编辑我的代码,包括所有涉及的参数和变量:

I edit my code including all the parameters and variables involved:

(D是从Python导入的numpy矩阵)

(D is a numpy matrix imported from Python)

import pyomo from pyomo.environ import * from array import * import numpy as np import scipy as sp from diff_matrix import D ##N=10???? print(D) m =ConcreteModel() ... m.n = Param(initialize = 10, within = Integers) m.Ns = Set(initialize = range(0,value(m.n))) m.x1 = Var(m.N, domain = Reals) m.D = Param(m.N, m.N, initialize=D) m.f_x1 = Var(m.N) def f_x1_definition(model,i): return m.f_x1[i] == sum(m.x1[j]*m.D[i,j] for j in range(value(m.n))) m.f_x1_const = Constraint(m.Ns, rule = f_x1_definition)

但是我收到下一个错误:

But I get the next error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

有帮助吗?

推荐答案

最简单的方法是仅使用Python sum()函数而不是Pyomo summation()函数:

The simplest thing is to just use the Python sum() function instead of the Pyomo summation() function:

def f_x1_definition(model,i): return model.f_x1[i] == sum(model.x1[j]*model.D[i,j] for j in range(value(model.n)))

此外,请注意,我颠倒了Pyomo Var(m.x1)和矩阵(m.D)的顺序.根据您的其他问题(将矩阵从Python导入Pyomo ) ,我假设矩阵是NumPy矩阵.当将NumPy值和Pyomo分量(Var或Param)相乘时,请始终将Pyomo对象放在第一位.这是由于在当前版本的Pyomo(至少达到5.1)中NumPy运算符重载与Pyomo运算符重载之间发生冲突.

Also, note that I reversed the order of the Pyomo Var (m.x1) and the matrix (m.D). Based on your other questions (Importing a matrix from Python to Pyomo), I am assuming that the matrix is a NumPy matrix. When multiplying a NumPy value and a Pyomo component (Var or Param), always put the Pyomo object first. This is due to a conflict between the NumPy operator overloading and the Pyomo operator overloading in current versions of Pyomo (up through at least 5.1).

编辑1 :有关反转操作数顺序的注意事项:在您最初的问题中,尚不清楚将m.D定义为Pyomo Param.表达式中Pyomo对象的顺序无关紧要.当将NumPy对象与Pyomo组件相乘时,上述操作符重载问题仅是 .此外,此时(直到Pyomo 5.1为止),Pyomo不支持矩阵代数-即像矩阵矩阵或矩阵向量乘积之类的运算.由于每个表达式都是标量表达式,所以交换运算中的项的顺序(+,*)不会改变表达式的含义.

EDIT 1: Note on reversing the order of operands: in your original question, it was not clear that m.D was being defined as a Pyomo Param. There is no concern with the order of Pyomo objects in expressions. The operator overloading problem mentioned above is only when multiplying NumPy objects with Pyomo components. Further, at this time (up through Pyomo 5.1), Pyomo does not support matrix algebra - that is, operations like matrix-matrix or matrix-vector products. Since every expression is a scalar expression, the ordering of the terms in a commutative operation (+, *) does not change the meaning of the expression.

编辑2 :您的错误与最初发布的sum/summation无关.问题在于您如何初始化参数.目前(直到Pyomo 5.1),您不能直接从numpy.ndarray初始化Param.您需要先将NumPy对象转换为Python字典,例如:

EDIT 2: Your error has nothing to do with the sum/summation you originally posted. The problem is with how you are initializing your Param. At this time (up through Pyomo 5.1), you cannot directly initialize a Param from a numpy.ndarray. You need to first convert the NumPy object into a Python dictionary with something like:

m.D = Param(m.N, m.N, initialize=dict(((i,j),D[i,j]) for i in m.N for j in m.N))

更多推荐

向量对矩阵乘积的热求和

本文发布于:2023-11-30 12:20:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649925.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:乘积   向量   矩阵

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!