Haskell-通过导入调用矩阵模块
我需要用矩阵乘法解一个线性方程。 我使用拥抱,因此我尝试了以下方法:Haskell-通过导入调用矩阵模块,haskell,matrix,import,linear-algebra,Haskell,Matrix,Import,Linear Algebra,我需要用矩阵乘法解一个线性方程。 我使用拥抱,因此我尝试了以下方法: import Matrix(multiplyMat) // more code follows 当我在控制台中调用它时,控制台告诉我: 找不到导入的模块“备忘录” 但是它在所有模块的目录中,那么为什么会出现错误呢 该文件名为Matrix.hs: -- Some simple Hugs programs for manipulating matrices. -- module Matrix where import Lis
import Matrix(multiplyMat)
// more code follows
当我在控制台中调用它时,控制台告诉我:
找不到导入的模块“备忘录”
但是它在所有模块的目录中,那么为什么会出现错误呢
该文件名为Matrix.hs:
-- Some simple Hugs programs for manipulating matrices.
--
module Matrix where
import List
type Matrix k = [Row k] -- matrix represented by a list of its rows
type Row k = [k] -- a row represented by a list of literals
-- General utility functions:
shapeMat :: Matrix k -> (Int, Int)
shapeMat mat = (rows mat, cols mat)
rows :: Matrix k -> Int
rows mat = length mat
cols :: Matrix k -> Int
cols mat = length (head mat)
idMat :: Int -> Matrix Int
idMat 0 = []
idMat (n+1) = [1:replicate n 0] ++ map (0:) (idMat n)
-- Matrix multiplication:
multiplyMat :: Matrix Int -> Matrix Int -> Matrix Int
multiplyMat a b | cols a==rows b = [[row `dot` col | col<-b'] | row<-a]
| otherwise = error "incompatible matrices"
where v `dot` w = sum (zipWith (*) v w)
b' = transpose b
-- An attempt to implement the standard algorithm for converting a matrix
-- to echelon form...
echelon :: Matrix Int -> Matrix Int
echelon rs
| null rs || null (head rs) = rs
| null rs2 = map (0:) (echelon (map tail rs))
| otherwise = piv : map (0:) (echelon rs')
where rs' = map (adjust piv) (rs1++rs3)
(rs1,rs2) = span leadZero rs
leadZero (n:_) = n==0
(piv:rs3) = rs2
-- To find the echelon form of a matrix represented by a list of rows rs:
--
-- {first line in definition of echelon}:
-- If either the number of rows or the number of columns in the matrix
-- is zero (i.e. if null rs || null (head rs)), then the matrix is
-- already in echelon form.
--
-- {definition of rs1, rs2, leadZero in where clause}:
-- Otherwise, split the matrix into two submatrices rs1 and rs2 such that
-- rs1 ++ rs2 == rs and all of the rows in rs1 begin with a zero.
--
-- {second line in definition of echelon}:
-- If rs2 is empty (i.e. if null rs2) then every row begins with a zero
-- and the echelon form of rs can be found by adding a zero on to the
-- front of each row in the echelon form of (map tail rs).
--
-- {Third line in definition of echelon, and definition of piv, rs3}:
-- Otherwise, the first row of rs2 (denoted piv) contains a non-zero
-- leading coefficient. After moving this row to the top of the matrix
-- the original matrix becomes piv:(rs1++rs3).
-- By subtracting suitable multiples of piv from (suitable multiples of)
-- each row in (rs1++rs3) {see definition of adjust below}, we obtain a
-- matrix of the form:
--
-- <----- piv ------>
-- __________________
-- 0 |
-- . |
-- . | rs' where rs' = map (adjust piv) (rs1++rs3)
-- . |
-- 0 |
--
-- whose echelon form is piv : map (0:) (echelon rs').
--
adjust :: Num a => Row a -> Row a -> Row a
adjust (m:ms) (n:ns) = zipWith (-) (map (n*) ms) (map (m*) ns)
-- A more specialised version of this, for matrices of integers, uses the
-- greatest common divisor function gcd in an attempt to try and avoid
-- result matrices with very large coefficients:
--
-- (I'm not sure this is really worth the trouble!)
adjust' :: Row Int -> Row Int -> Row Int
adjust' (m:ms) (n:ns) = if g==0 then ns
else zipWith (\x y -> b*y - a*x) ms ns
where g = gcd m n
a = n `div` g
b = m `div` g
-- end!!
——一些用于操作矩阵的简单Hugs程序。
--
模块矩阵在哪里
导入列表
类型矩阵k=[Row k]--由其行列表表示的矩阵
键入Row k=[k]——由一系列文字表示的行
--一般实用功能:
矩阵k->(Int,Int)
形状马特垫=(行垫、列垫)
行::矩阵k->Int
行垫=长度垫
矩阵k->Int
cols mat=长度(头垫)
idMat::Int->Matrix Int
idMat 0=[]
idMat(n+1)=[1:复制n0]++map(0:)(idMat n)
--矩阵乘法:
乘法器::矩阵整数->矩阵整数->矩阵整数
乘法矩阵a b | cols a==行b=[[row`dot`col | col矩阵Int]
梯队
|空rs | |空(头部rs)=rs
|空rs2=map(0:)(梯队(map尾部rs))
|否则=piv:map(0:)(梯队rs')
其中rs'=映射(调整piv)(rs1++rs3)
(rs1,rs2)=跨度引线零rs
leadZero(n:_)=n==0
(piv:rs3)=rs2
--要查找由行列表rs表示的矩阵的梯队形式,请执行以下操作:
--
--{梯队定义的第一行}:
--如果矩阵中的行数或列数
--为零(即,如果为null rs | | null(head rs)),则矩阵为
--已经处于梯队状态。
--
--{where子句中rs1、rs2、leadZero的定义}:
--否则,将矩阵拆分为两个子矩阵rs1和rs2,以便
--rs1++rs2==rs,并且rs1中的所有行都以零开头。
--
--{梯队定义中的第二行}:
--如果rs2为空(即如果rs2为空),则每一行都以零开始
--rs的梯队形式可以通过在
--以梯队形式排列在每行的前面(地图尾rs)。
--
--{梯队定义中的第三行,以及piv的定义,rs3}:
--否则,rs2的第一行(表示为piv)包含一个非零值
--前导系数。将此行移动到矩阵顶部后
--原始矩阵变成piv:(rs1++rs3)。
--通过从(适当的piv倍数)中减去适当的piv倍数
--在(rs1++rs3){参见下面调整的定义}中的每一行,我们得到一个
--表格的矩阵:
--
--
-- __________________
-- 0 |
-- . |
--.| rs'其中rs'=映射(调整piv)(rs1++rs3)
-- . |
-- 0 |
--
--其梯队形式为piv:map(0:)(梯队rs')。
--
调整::Num a=>行a->行a->行a
调整(m:ms)(n:ns)=带(-)(映射(n*)ms)(映射(m*)ns)的zipWith
--更专业的版本是,对于整数矩阵,使用
--最大公约数函数gcd的一种尝试和避免
--系数非常大的结果矩阵:
--
--(我不确定这是否真的值得麻烦!)
调整“::行整数->行整数->行整数
调整’(m:ms)(n:ns)=如果g==0,则ns
else zipWith(\x y->b*y-a*x)ms ns
式中,g=gcd m n
a=n`div`g
b=m`div`g
--结束!!
您的矩阵
文件不导出任何内容
要导出,请编写如下标题:
module Module (function, value, Type(Constructor))
你有什么模块,它们的文件名是什么,它们里面有什么?请试着编一个最小的可编译的例子,然后你自己可能会发现错误。-除此之外…我最近一直在重复我自己为什么你们要使用Hugs?Haskell平台和
cabal
使模块管理变得更容易…我更新了,你能看一下吗?这在Haskell平台上编译得很好(好吧,在将导入列表
更改为导入数据.列表
,并启用{-#语言NPlusKPatterns}
)之后)。显然,你的Hugs设置有问题。当你加载一个简单的main=print“你好,世界!”
module?你能告诉我启用代码应该放在哪里吗?谢谢你的帮助:)LANGUAGE
pragmas放在源文件的顶部。但这与你的问题无关,只是默认情况下GHC禁止n+k
模式,因为它们造成的混乱多于帮助。