Operating the linear algebra library in Haskell and using the matrix and gnuplot Plot Method

Source: Internet
Author: User

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

 

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.