gpt4 book ai didi

haskell - 在haskell中求反矩阵?

转载 作者:行者123 更新时间:2023-12-02 10:10:07 25 4
gpt4 key购买 nike

如果我使用vector包,我怎样才能得到矩阵的负数?仅仅“减”是行不通的。

所以来自:

 1 2 3
3 4 5
6 7 8

我想要得到:

 -1 -2 -3
-3 -4 -5
-6 -7 -8

不,乘以 -1 也不起作用:

let m = fromList [[10,20,30], [11,12, 13]]
Prelude Data.Vector> :t m
m :: Vector [Integer]
Prelude Data.Vector> (-1)*m


<interactive>:1:3:
No instance for (Num (Vector [Integer]))
arising from the literal `1'
Possible fix:
add an instance declaration for (Num (Vector [Integer]))
In the expression: 1
In the first argument of `(*)', namely `(- 1)'
In the expression: (- 1) * m

最佳答案

我认为您对矢量库的用途感到困惑。其描述为:

An efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework .

所以它只是一个数组类 - 如果您想对其执行矩阵运算,您必须自己编写解决方案。

您真正想要的是使用真正的矩阵库。 hmatrix是一个执行此操作的库。首先使用 hmatrix installation instructions 安装它.

这是使用 hmatrix 编写程序的方式:

> import Data.Packed.Matrix
> let matrix = (3><3)[1,2,3,3,4,5,6,7,8]
> mapMatrix negate matrix
(3><3)
[ -1.0, -2.0, -3.0
, -3.0, -4.0, -5.0
, -6.0, -7.0, -8.0 ]

库中有大量矩阵运算,只需阅读 Data.Packed.MatrixNumeric.Container文档

关于haskell - 在haskell中求反矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8289053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com