我需要以编程方式解决C,Objective C或(如果需要)C++中的线性方程组.
这是方程的一个例子:
-44.3940 = a * 50.0 + b * 37.0 + tx
-45.3049 = a * 43.0 + b * 39.0 + tx
-44.9594 = a * 52.0 + b * 41.0 + tx
Run Code Online (Sandbox Code Playgroud)
由此,我想获得最好的逼近a
,b
以及tx
.
我不使用tile,而是使用sf :: Vertex绘制的多维数据集.每个立方体有6个边,每个边有4个点.
所以我只需要cubes[numCube].sides()[numSide]....
选择一个方面.
我创建了立方体layer.cpp:
for(int J = 0; J < mapSize; J++)
{
for(int I = 0; I < mapSize; I++)
{
x = (J - I) * (cubeSize/2);
y = (J + I) * (cubeSize/4);
c = new cube(cubeSize, x, y, z, I, J);
cs.push_back(*c);
}
}
Run Code Online (Sandbox Code Playgroud)
在cube.cpp中我创建了侧面,然后,在sides.cpp中,我像这样计算每个点的坐标:
switch(typeSide)
{
case 0://DOWN_SIDE
light = 1;
tmp_x = x + (size/2);
tmp_y = y + (size/2);
p0 = new point(tmp_x, tmp_y, tmp_z);
tmp_x = x + size; …
Run Code Online (Sandbox Code Playgroud) 嗨,我想问我的同伴python用户如何执行他们的线性拟合.
我一直在搜索方法/库的最后两周来执行此任务,我想分享我的经验:
如果您想基于最小二乘法执行线性拟合,您有很多选择.例如,您可以在numpy和scipy中找到类.我自己选择了由linfit呈现的那个(遵循IDL中linfit函数的设计):
http://nbviewer.ipython.org/github/djpine/linfit/blob/master/linfit.ipynb
此方法假定您在y轴坐标中引入sigmas以适合您的数据.
但是,如果您已经量化了x轴和y轴的不确定性,则没有太多选项.(在主要的python科学图书馆中没有IDL"Fitexy"等价物).到目前为止,我只找到了"kmpfit"库来执行此任务.幸运的是,它有一个非常完整的网站,描述了它的所有功能:
https://github.com/josephmeiring/kmpfit http://www.astro.rug.nl/software/kapteyn/kmpfittutorial.html#
如果有人知道其他方法,我也很想知道它们.
无论如何,我希望这会有所帮助.
在“sympy”中表达以下内容的最合适方式是什么:
对样本 'x[i]' 的总和,其中 'i' 从具体的 0 到符号的 'N'。'x[i]' 本身应该是象征性的,即总是作为变量出现。
目标是在线性方程组中使用这些表达式。
示例(平凡最小二乘近似):
给定一组样本 (x[i], y[i]),它们应该位于由 'y = m*x + a' 给出的一条线上。即,估计线由“m”和“a”确定。样本和估计线之间的误差可以由下式给出
error(m, a) = sum((m * x[i] + a - y[i])**2, start_i=0, end_i=N)
Run Code Online (Sandbox Code Playgroud)
现在,在导数 'd/dm error(m,a)' 和 'd/da error(m,a)' 中搜索零转换提供了最小距离。我怎样才能找到 sympy 的解决方案?
我有一个线性不等式/等式系统在Matlab中解决我使用linprog
.由于一些不等式是严格的,我使用一个非常小的常数eps
来得到严格的包含,如这里所解释的
solve
下面的函数在提供了一个值后解决了系统问题eps
.
function pj=solve(eps)
%Inequalities
%x(1)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)<=0;
%x(2)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)<=0;
%x(3)-x(7)-x(10)-x(12)-x(14)-x(16)-x(17)-x(18)-x(19)<=0;
%x(4)-x(8)-x(11)-x(13)-x(14)-x(15)-x(16)-x(18)-x(19)<=0;
%x(1)+x(2)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)<=0;
%x(1)+x(3)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(7)-x(12)-x(14)-x(18)<=0;
%x(1)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(8)-x(13)-x(14)-x(18)<=0;
%x(2)+x(3)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(7)-x(10)-x(14)-x(16)<=0;
%x(2)+x(4)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(8)-x(11)-x(14)-x(16)<=0;
%x(3)+x(4)-x(7)-x(10)-x(12)-x(14)-x(16)-x(17)-x(18)-x(19)-...
% x(8)-x(11)-x(13)-x(15)<=0;
%x(1)+x(2)+x(3)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)-...
% x(7)-x(14)<=0;
%x(1)+x(2)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)-...
% x(8)-x(14)<=0;
%x(1)+x(3)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(7)-x(12)-x(14)-x(18)-...
% x(8)-x(13)<=0;
%x(2)+x(3)+x(4)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(7)-x(10)-x(14)-x(16)-...
% x(8)-x(11)<=0;
%Equalities
%x(1)+x(2)+x(3)+x(4)=1;
%x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)+x(12)+x(13)+x(14)+x(15)+x(16)+x(17)+x(18)+x(19)=1;
%I also want each component of x to be different from 1 and 0 (strictly included between 1 and 0 given …
Run Code Online (Sandbox Code Playgroud) 我开始使用PETSc库来并行求解线性方程组.我已经安装了所有软件包,构建并成功运行了petsc/src/ksp/ksp/examples/tutorials /文件夹中的示例,例如ex.c
但我无法理解如何通过从文件中读取矩阵来填充矩阵A,X和B.
这里我提供了ex2.c文件中的代码:
/* Program usage: mpiexec -n <procs> ex2 [-help] [all PETSc options] */
static char help[] = "Solves a linear system in parallel with KSP.\n\
Input parameters include:\n\
-random_exact_sol : use a random exact solution vector\n\
-view_exact_sol : write exact solution vector to stdout\n\
-m <mesh_x> : number of mesh points in x-direction\n\
-n <mesh_n> : number of mesh points in y-direction\n\n";
/*T
Concepts: KSP^basic parallel example;
Concepts: KSP^Laplacian, 2d
Concepts: Laplacian, 2d
Processors: n
T*/
/* …
Run Code Online (Sandbox Code Playgroud) 我试图将下面的等式转换为编程代码.目的是找到两条线的交叉点.并告诉pront
(y1 - y2)x - (x1 - x2)y =(y1 - y2)x1 - (x1 - x2)y1
(y3 - y4)x - (x3 - x4)y =(y3 - y4)x3 - (x3 - x4)y3
我被告知使用cramers规则,但cramers规则有6个diff变量.我将从4个不同的点开始作为8个变量(x1,y1,x2,y2,x3,y3,x4,y4)
我正在使用Java.任何帮助,将不胜感激.本网站上的所有问题都是针对不同类型的线性方程式,代码很复杂,我找不到任何与我相关的东西.
这就是我所拥有的,并不多,但从上面的等式转变为可编程的东西真的让我感到困惑.
import java.util.Scanner;
public class E325 {
public static void main(String[] args) {
/*
* The purpose of this program is to find the intersect
* of two lines given by the user by four points
*
* Get the four points. x1,y1 x2,y2 x3,y3 x4,y4
*/
Scanner input …
Run Code Online (Sandbox Code Playgroud) 我的问题是我有一个矩阵,其中所有行的总和,以及所有列的总和为零.所有数字都舍入为x小数.
然后我将整个矩阵乘以0到1之间的数字(例如1/6)并将所有数字四舍五入为x小数.现在我无法确定行和列的总和是否为零.我希望在最小可能的调整(或至少非常小的调整)下,总和再次为零
是否存在可以解决此类问题的算法?
示例(非常简单):矩阵:
200 -200 0
400 400 -800
-600 -200 800
Run Code Online (Sandbox Code Playgroud)
round2((1/6)*矩阵)
33.33 -33.33 0
66.67 66.67 -133.33
-100 -33.33 133.33
Run Code Online (Sandbox Code Playgroud) 有没有算法来解决在不同模空间中表达的方程组?例如,考虑这个方程组:
(x1 + x2 ) % 2 = 0
( x2 + x3) % 2 = 0
(x1 + x2 + x3) % 3 = 2
Run Code Online (Sandbox Code Playgroud)
该系统的解决方案之一是:
x1 = 0
x2 = 2
x3 = 0
Run Code Online (Sandbox Code Playgroud)
我怎么能算术地找到这个解决方案(不使用暴力算法)?
谢谢
我想举一个简单的例子来说明我想做的事情。我有一些在此步骤之前计算并存储在矩阵中的数据。在这个简单的例子中,我们只取两个 2x2 矩阵。它们看起来像这样:
A1=np.array([[1, 2], [0.5, 1.5]])
A2=np.array([[0.5, 1.2], [1.3, 2]])
Run Code Online (Sandbox Code Playgroud)
我也有解向量 b,它可能看起来像:
b=[4, 3]
Run Code Online (Sandbox Code Playgroud)
现在我想解决线性方程组 Ax=b 的所有可能组合,这些组合可以从存储在矩阵 A1 和 A2 中的数据创建。在两个矩阵中,列的数据相互连接。因此,从我的示例中求解的可能线性方程组如下所示:
LES1:
1 * x1 + 0.5 * x2 = 4
0.5 * x1 + 1.3 * x2 = 3
Run Code Online (Sandbox Code Playgroud)
LES2:
1 * x1 + 1.2 * x2 = 4
0.5 * x1 + 2 * x2 = 3
Run Code Online (Sandbox Code Playgroud)
LES3:
2 * x1 + 0.5 * x2 = 4
1.5 * x1 + 1.3 * x2 = 3
Run Code Online (Sandbox Code Playgroud)
LES4: …