Haskell's native data structure is not efficient for large-scale mathematical operations. Using list to store vectors or matrices is inefficient. Fortunately, Haskell has a wealth of third-party libraries that can be used to complete this operation with a powerful professional mathematical library. We will introduce the hmatrix library today.
1. Installation
Development Environment: MacOSX 10.5
1. Install the dependent Library
GSL (
GNU Scientific Library
), Go to the GNU website to download the source code and compile it by yourself.
2. Install hmatrix, gnuplot, and so on. Use cabal install.
=====
II. Introduction
1. Matrix generation and operations
Data. Packed. Matrix
(> <)
: Element
A => int
-> Int
-> [A]-> Matrix
A
> <This operator is used to define the rows and columns of a matrix. Its shape is like the X operator.
Test:
M1 = (3> <4) [1 ..]: matrix double
The corresponding matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
2. Modify the value of the matrix element in-place
Use status monad
Import data. Packed. St
Import Control. monad. St
Testupdatemat: matrix double
Testupdatemat = runstmatrix $ do
M <-newmatrix 0 3 4
Writematrix M 0 0 1
Writematrix M 2 3 4
Return m
The matrix returned by testupdatemat, newmatrix first creates a 3x4 matrix with 0 values, and writematrix M 0 0 1 modifies the value of M (0, 0) to 1, writematrix M 2 3 4 change the (2, 3) position value to 4
The final result is:
1 0 0 0
0 0 0 0
0 0 0 4
3. Plot Using Plot
Directly add the code and, But I have explained more.
Import graphics. Plot
Vector xs = fromlist Xs: Vector Double
Plot2d F n = mesh (f x Y) Where
(X, y) = meshdom Range
Range = linspace N (-2, 2)
Sombrero x y = exp (-R2) * Cos (2 * R2)
Where r2 = x * x + y * y
F x = SiN x + 0.5 * sin (5 * X)
Main = do
Let X = linspace 1000 (-4, 4)
Mplot [f x]
Plot2d sombrero 40