import sympy as S
F = S.FiniteField(101)
Run Code Online (Sandbox Code Playgroud)
当我打电话时,f = S.poly(y ** 2 - x ** 3 - x - 1,F)我收到以下错误:
'FiniteField'对象没有属性'is_commutative'
但有限域是定义的可交换的!所以我不确定这个错误应该是什么意思!
有没有人遇到过这个?如何在有限域上声明多项式?
我正在尝试cart :: [[a]] -> [[a]]为笛卡尔积编写一个通用函数,以便生成从 0 到 7 的 9 元组数字集(9 个元素的列表,而不是实际的 9 元组)。我已经编写了几个语法相似的函数,但它们的性能却大不相同。
cart :: [[a]] -> [[a]]
cart [] = [[]]
cart (xs:xss) = [x:ys | x <- xs, ys <- cart xss]
cart' :: [[a]] -> [[a]] -- Reverse binding
cart' [] = [[]]
cart' (xs:xss) = [x:ys | ys <- cart' xss, x <- xs]
xs = [0..7]
length $ cart $ replicate 9 xs -- This is slow (4.1s) and memory hungry (1916MB …Run Code Online (Sandbox Code Playgroud) 我正在使用最速下降法来找出具有5x5希尔伯特矩阵的线性系统的解.我相信代码很好,因为它给了我正确的答案.
我的问题是:
我认为这需要太多的迭代才能得到正确的答案.我相信我可能错过了算法中的一些东西,但我不知道此时是什么.
我不确定这是否是实现算法的最有效方法,另外,选择"tol"有点令人困惑.
任何有关这些的见解将不胜感激(尤其是1.).谢谢!
% Method of Steepest Descent with tol 10^-6
h = hilb(5); %Hilbert 5x5 matrix
b = [1;1;1;1;1]; %solution matrix
solution = zeros(d,1); %Initialization
residual = h*solution - b;
tol = 10^(-6)
count = 0;
while residual'*residual > tol;
roe = (residual'*residual)/(residual'*h*residual);
solution = solution - roe*residual;
residual = h*solution - b;
count = count + 1;
end
count
solution
%Method of Steepest Descent with tol 10^-12
solution = zeros(d,1);
residual = h*solution - …Run Code Online (Sandbox Code Playgroud) optimization matlab mathematical-optimization numerical-methods gradient-descent
假设我的约束是矩阵变量的第一列和第三列的乘积大于一。如何在 CVXPY 中实施?例子:
w = Variable(4,3)
Run Code Online (Sandbox Code Playgroud)
在 Matlab 中,我的约束是:
w(:,1)'*w(:,3)>1
Run Code Online (Sandbox Code Playgroud)
我如何在 CVXPY 中实现它?或者我们可以在 CVXPY 下执行点积吗?numpy.dotCVXPY 不支持。
python mathematical-optimization convex-optimization python-2.7 cvxpy
我想将受 LMI 约束的优化问题从 Matlab 转换为 Python。在阅读 CVXPY 文档时,我发现我可以通过创建矩阵变量并添加相应约束来定义 LMI 约束问题。但是,我需要使用以下块 LMI,而不是通过简单的 LMI 来约束问题:
其中P,L是矩阵变量,gamma是标量。其他符号是动态系统的状态空间矩阵。
是否可以在 CVXPY 中使用这种 LMI 作为约束?如果没有,是否还有其他工具可以让我在 Python 中解决这个问题?
在 cvxpy 中解决优化问题时,是否有一种很好的方法可以通过替换优化变量的实际值来检查约束是否有效?
我有一个复杂的优化问题(100 多个约束),但我知道最佳解决方案应该是什么。但是, cvxpy 失败并显示错误消息ValueError: Rank(A) < p or Rank([G; A]) < n
我认为这是因为我在其中一个约束中有一个错字,使它们不一致。有没有一种很好的方法来替换变量的实际值,以查看违反了哪些约束(因为它们可能有拼写错误)?
我的实际问题很复杂,所以我做了一个简单的例子:
from cvxpy import *
x = variable(name='x')
y = variable(name='y')
c1 = greater_equals(x, 1.)
c2 = greater_equals(y, 1.)
c3 = less_equals(x + y, -4.) # typo: -4 should be +4
p = program(maximize(2. * x + y), [c1, c2, c3])
p.solve()
Run Code Online (Sandbox Code Playgroud)
将-4在约束c3应该是+4。这失败并显示错误消息:Certificate of primal infeasibility found.
如果我输入,p.show()我得到:
maximize 2.0*x + y
subject to …Run Code Online (Sandbox Code Playgroud) 我试图在JSON中使用矩阵样式数据,但它似乎不起作用.谁能帮我理解我做错了什么?
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 44,
"max_score": 1,
"hits": [
{
"_index": "transactions",
"_type": "transaction",
"_id": "trans0007",
"_score": 1,
"_source": {
"fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0],
[0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0],
[0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
[0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
",
"fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
"fundColor": ["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"] …Run Code Online (Sandbox Code Playgroud) 给定两个数组A(shape:MXC)和B(shape:NXC),有没有一种方法可以A在B不使用循环的情况下从每一行中减去每一行?最终输出将是形状(MNXC)。
A = np.array([[ 1, 2, 3],
[100, 200, 300]])
B = np.array([[ 10, 20, 30],
[1000, 2000, 3000],
[ -10, -20, -2]])
Run Code Online (Sandbox Code Playgroud)
所需结果(可以具有其他形状)(已编辑):
array([[ -9, -18, -27],
[-999, -1998, -2997],
[ 11, 22, 5],
[ 90, 180, 270],
[-900, -1800, -2700],
[ 110, 220, 302]])
Shape: 6 X 3
Run Code Online (Sandbox Code Playgroud)
(循环太慢,“外部”减去每个元素而不是每一行)
我对 CVXPY 真的很陌生。我正在尝试解决8 个皇后问题,即将 8 个国际象棋皇后放在 8 x 8 棋盘上,这样两个皇后就不会互相威胁。据我了解,约束应该是:
此外,目标函数应该是:最大化矩阵的 2-范数(这样我们只能得到 1 和 0,因为我们也可以得到 s 的和为 1 float,但是 1 与 0 的范数大于浮点数的范数) 0 到 1 之间且总和为 1(例如:0.8^2+0.2^2 < 1^2+0^2)。
CVXPY 中可以解决此类问题吗?我对如何在 CVXPY 中形成约束和目标函数一无所知,但这是我最初的初步尝试(我立即收到“DCP 错误”,所以我没有理由继续,但仍然如此):
from cvxpy import *
x=Variable(shape=(9,9), name='board')
obj = Maximize(norm(x))
const = [sum(x, axis=1)==1]
prob=Problem(objective=obj,constraints=const)
prob.solve()
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!!!
python optimization mathematical-optimization n-queens cvxpy
我有:
-一组已知大小的点(在我的情况下,只有6个点)
-一条以x = s + t * r为特征的线,其中x,s和r是3D矢量
I need to find the point closest to the given line. The actual distance does not matter to me.
I had a look at several different questions that seem related (including this one) and know how to solve this on paper from my highschool math classes. But I cannot find a solution without calculating every distance, and I am sure there has to be a better/faster way. Performance is absolutely crucial …
python ×6
cvxpy ×4
optimization ×3
matlab ×2
matrix ×2
c++ ×1
finite-field ×1
haskell ×1
json ×1
n-queens ×1
numpy ×1
performance ×1
polynomials ×1
python-2.7 ×1
sympy ×1