numpy vs mldivide,"\" Matlab运算符 [英] Numpy vs mldivide,"\" matlab operator

查看:165
本文介绍了numpy vs mldivide,"\" Matlab运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A \ B提供了一种特殊的解决方案,而numpy.linalg.lstsq却没有.

A \ B in matlab gives a special solution while numpy.linalg.lstsq doesn't.

A = [1 2 0; 0 4 3];
b = [8; 18];
c_mldivide = A \ b
c_mldivide =

                 0
                 4
  0.66666666666667
 c_lstsq = np.linalg.lstsq([[1 ,2, 0],[0, 4, 3]],[[8],[18]])
 print c_lstsq
 c_lstsq = (array([[ 0.91803279],
                   [ 3.54098361],
                   [ 1.27868852]]), array([], dtype=float64), 2, array([ 5.27316304,1.48113184]))

  1. matlab中的mldivide A \ B如何提供特殊的解决方案?
  2. 此解决方案对实现计算精度有用吗?
  3. 为什么这个解决方案很特别?您如何在numpy中实现呢?
  1. How does mldivide A \ B in matlab give a special solution?
  2. Is this solution usefull in achieving computational accuracy?
  3. Why is this solution special and how might you implement it in numpy?

推荐答案

对于像您这样的不确定系统(等级小于变量数),mldivide返回具有尽可能多的零值.哪个变量将被设置为零取决于其任意选择.

For under-determined systems such as yours (rank is less than the number of variables), mldivide returns a solution with as many zero values as possible. Which of the variables will be set to zero is up to its arbitrary choice.

相反,在这种情况下,lstsq方法返回最小范数的解:也就是说,在无穷大的精确解中,它将选择平方和最小的那个.的变量.

In contrast, the lstsq method returns the solution of minimal norm in such cases: that is, among the infinite family of exact solutions it will pick the one that has the smallest sum of squares of the variables.

因此,Matlab的特殊"解决方案在某种程度上是任意的:在此问题中,可以将三个变量中的任何一个设置为零.实际上,NumPy提供的解决方案更为特殊:存在一个独特的最小范数解决方案

So, the "special" solution of Matlab is somewhat arbitrary: one can set any of the three variables to zero in this problem. The solution given by NumPy is in fact more special: there is a unique minimal-norm solution

哪种解决方案更适合您的目的取决于您的目的.解的非唯一性通常是重新考虑您的方程式方法的原因.但是,正如您所问的那样,这里是产生Matlab类型解决方案的NumPy代码.

Which solution is better for your purpose depends on what your purpose is. The non-uniqueness of solution is usually a reason to rethink your approach to the equations. But since you asked, here is NumPy code that produces Matlab-type solutions.

import numpy as np
from itertools import combinations
A = np.matrix([[1 ,2, 0],[0, 4, 3]])
b = np.matrix([[8],[18]])

num_vars = A.shape[1]
rank = np.linalg.matrix_rank(A)
if rank == num_vars:              
    sol = np.linalg.lstsq(A, b)[0]    # not under-determined
else:
    for nz in combinations(range(num_vars), rank):    # the variables not set to zero
        try: 
            sol = np.zeros((num_vars, 1))  
            sol[nz, :] = np.asarray(np.linalg.solve(A[:, nz], b))
            print(sol)
        except np.linalg.LinAlgError:     
            pass                    # picked bad variables, can't solve

对于您的示例,它输出三个特殊"解决方案,最后一个是Matlab选择的解决方案.

For your example it outputs three "special" solutions, the last of which is what Matlab chooses.

[[-1. ]
 [ 4.5]
 [ 0. ]]

[[ 8.]
 [ 0.]
 [ 6.]]

[[ 0.        ]
 [ 4.        ]
 [ 0.66666667]] 

这篇关于numpy vs mldivide,"\" Matlab运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆