分享

各位老师,我用breeze的pinv求矩阵伪逆,为什么结果是错的?

1601365 2018-7-14 17:25:22 发表于 疑问解答 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 6 7222
各位老师,我用breeze的pinv求矩阵伪逆,为什么结果是错的?下面是一段测试代码:
package AE_LOC

import breeze.linalg.{DenseMatrix, eig, inv, pinv}

/**
  * Created by Administrator on 2018/7/11.
  */
object Test_Matrix {
  def main(args: Array[String]): Unit = {

      //取MATLAB中第二次计算的结果
      val A=DenseMatrix((1.0,0.000052849468204,-0.000141077142447,-0.000089904809261),
      (1.0,0.000052913176697,-0.000140978731412,-0.000090021631624),
      (1.0,0.000053199579186,-0.000140971328619,-0.000089864288907),
      (1.0,0.000053166285325,-0.000140899813861,-0.000089996053789),
      (1.0,0.000053368539372,-0.000140830212164,-0.000089985284831),
      (1.0,0.000053031908518,-0.000141110407135,-0.000089745032264),
      (1.0,0.000052959039354,-0.000141090707018,-0.000089819005131),
      (1.0,0.000052992075982,-0.000140807429379,-0.000090243054323))

    val deltaw=pinv(A.t*A)//*A.t*r
    println(deltaw)
  }
}
求解结果如下:
七月 14, 2018 5:12:46 下午 com.github.fommil.netlib.BLAS <clinit>
警告: Failed to load implementation from: com.github.fommil.netlib.NativeSystemBLAS
七月 14, 2018 5:12:46 下午 com.github.fommil.netlib.BLAS <clinit>
警告: Failed to load implementation from: com.github.fommil.netlib.NativeRefBLAS
七月 14, 2018 5:12:47 下午 com.github.fommil.netlib.LAPACK <clinit>
警告: Failed to load implementation from: com.github.fommil.netlib.NativeSystemLAPACK
七月 14, 2018 5:12:47 下午 com.github.fommil.netlib.LAPACK <clinit>
警告: Failed to load implementation from: com.github.fommil.netlib.NativeRefLAPACK
4.872723391844739E11   -8.402618428318382E14   2.2312732735908435E15   1.4246530975093118E15   
-8.402618428281145E14  1.44896791033931622E18  -3.8476501203625242E18  -2.4566979259979945E18  
2.2312732735986278E15  -3.8476501203929989E18  1.0217245392080683E19   6.5236404952027802E18   
1.424653097499306E15   -2.4566979259916278E18  6.5236404951342039E18   4.1653054625770337E18   

Process finished with exit code 0

而用MATLAB或Python求解结果是一致的:
1.0e+12 *

   0.000000000000136   0.000000025810710   0.000000120865941  -0.000000174075492
   0.000000025810710   4.142477448797149   0.652950358936210   1.420594988073896
   0.000000120865942   0.652950358931182   1.316103334637498  -1.676153252853749
  -0.000000174075494   1.420594988077356  -1.676153252871454   3.463037932613488

已有(6)人评论

跳转到指定楼层
jixianqiuxue 发表于 2018-7-14 18:36:56
没遇到过。不过先把本地库的加载问题解决掉。看看是不是这个原因。

回复

使用道具 举报

1601365 发表于 2018-7-14 21:09:49
jixianqiuxue 发表于 2018-7-14 18:36
没遇到过。不过先把本地库的加载问题解决掉。看看是不是这个原因。

我已经在Intellij IDEA的pom文件中已经添加了如下配置,为什么还是加载失败呢?
<dependency>
    <groupId>com.github.fommil.netlib</groupId>
    <artifactId>core</artifactId>
    <version>1.1.2</version>
</dependency>
回复

使用道具 举报

1601365 发表于 2018-7-14 22:08:35
这几个都配置了,还是出现这种情况
<dependency>
                <groupId>com.github.fommil.netlib</groupId>
                <artifactId>core</artifactId>
                <version>1.1.2</version>
            </dependency>

            <dependency>
                <groupId>com.github.fommil.netlib</groupId>
                <artifactId>generator</artifactId>
                <version>1.1.2</version>
            </dependency>

            <dependency>
                <groupId>com.github.fommil.netlib</groupId>
                <artifactId>native_ref-java</artifactId>
                <version>1.1</version>
            </dependency>

            <dependency>
                <groupId>com.github.fommil.netlib</groupId>
                <artifactId>native_system-java</artifactId>
                <version>1.1</version>
            </dependency>
回复

使用道具 举报

yaojiank 发表于 2018-7-15 17:38:08
1601365 发表于 2018-7-14 22:08
这几个都配置了,还是出现这种情况

                com.github.fommil.netlib

pinv(A)是什么结果?
回复

使用道具 举报

1601365 发表于 2018-7-16 09:57:18
yaojiank 发表于 2018-7-15 17:38
pinv(A)是什么结果?

咦?这时候求出的结果和MATLAB及Python计算结果是相同的,用Scala计算,有时候结果和MATLAB一致,有时候不一致,请问老师这到底是前面的警告问题还是其自身内部问题呀?都是:matrix([[ 1.25165348e+05, -2.56012166e+05, -2.32656154e+05, -4.07343401e+05,  2.88611209e+05,  2.31696497e+05,3.15802936e+04,  2.18959373e+05],
        [-2.16718525e+08,  4.40753618e+08,  4.01892796e+08,7.02848508e+08, -4.96370480e+08, -3.99461933e+08, -5.47717408e+07, -3.78172242e+08],
        [ 5.72796431e+08, -1.17229027e+09, -1.06540743e+09, -1.86502632e+09,  1.32203143e+09,  1.06042186e+09, 1.44170180e+08,  1.00330412e+09],
        [ 3.65976239e+08, -7.48963265e+08, -6.79737183e+08, -1.19109921e+09,  8.43892562e+08,  6.78312956e+08, 9.28343200e+07,  6.38783584e+08]])
回复

使用道具 举报

1601365 发表于 2018-7-16 20:55:19
而且我发现矩阵相乘也有问题,比如val M=DenseMatrix((1.0,2.0,3.0),(4.0,5.0,6.0))
    val N=DenseMatrix((1.0,2.0),(3.0,4.0),(5.0,6.0))
    val result=N*M,结果计算正确。但是下面的例子计算结果是错误的
    val A=DenseMatrix((1.0,0.0,0.000175438596491,0.0),
    (1.0,0.000071695151840,0.000095593535786,-0.000128453813713),
    (1.0,0.000161047509903,0.000069504714800,0.000003390473893),
    (1.0,0.000147342297325,0.000066541682663,-0.000068126008441),
    (1.0,0.000120984143693,0.000127033350877,0.000002016402395),
    (1.0,0.000149442735812,-0.000091800537713,0.000004269792452),
    (1.0,0.000171825300658,-0.000034365060132,0.000008591265033),
    (1.0,0.000072337784054,0.000045364373051,-0.000153258017065))
    val c=A.t
    val result1=c*A这个结果就是错误的,我现在开始怀疑是不是下面这些警告的问题,如果是应该怎么解决呢?我搜了好几天没整明白,我用的是Intellij IDEA,maven的pom文件应该怎么更改呢?跪求大神帮助呀
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条