矩陣運算和複數運算

2021-09-02 19:58:26 字數 4546 閱讀 6585

matrix.h

#pragma once

struct matrix

;void

initial_matrix

(struct matrix *m)

;void

mul_matrix

(struct matrix *m,

struct matrix *n)

;void

sub_matrix

(struct matrix *m,

struct matrix *n)

;void

add_matrix

(struct matrix *m,

struct matrix *n)

;void

input_matrix

(struct matrix *m)

;void

initial_matrix

(struct matrix *m)

;void

destroy_matrix

(struct matrix *m)

;void

display_matrix

(struct matrix *m)

;struct matrix transpose_matrix

(struct matrix *m)

;double

norm2_matrix

(struct matrix *m)

;double

max(

double

*v,int col)

;void

mul(

struct matrix *r,

double

*v);

matrix.cpp

#include

"matrix.h"

#include

#include

#include

// 矩陣乘法

void

mul_matrix

(struct matrix *m,

struct matrix *n)

struct matrix r;

r.row = m->row;

r.col = n->col;

initial_matrix

(&r)

;int i, j;

for(i =

0; i < r.row; i++)}

}display_matrix

(&r)

;destroy_matrix

(&r);}

// 矩陣減法

void

sub_matrix

(struct matrix *m,

struct matrix *n)

struct matrix r;

r.row = m->row;

r.col = m->col;

initial_matrix

(&r)

;int i, j;

for(i =

0; i < r.row; i++)}

display_matrix

(&r)

;destroy_matrix

(&r);}

// 矩陣加法

void

add_matrix

(struct matrix *m,

struct matrix *n)

struct matrix r;

r.row = m->row;

r.col = m->col;

initial_matrix

(&r)

;int i, j;

for(i =

0; i < r.row; i++)}

display_matrix

(&r)

;destroy_matrix

(&r);}

// 輸入矩陣

void

input_matrix

(struct matrix *m)}}

// 初始化矩陣,給矩陣分配記憶體

void

initial_matrix

(struct matrix *m)

}// 銷毀矩陣,釋放矩陣的記憶體

void

destroy_matrix

(struct matrix *m)

// 列印矩陣

void

display_matrix

(struct matrix *m)

printf

("\n");

}}// 矩陣的轉置

struct matrix transpose_matrix

(struct matrix *m)

}return r;

}// 矩陣2-範數

double

norm2_matrix

(struct matrix *m)}}

// 使用冪法求解 r 的最大特徵值

double

*v; v =

(double*)

malloc

(sizeof

(double

)*r.col)

;for

(i =

0; i < r.col; i++

)double m0 =

0, m1 = int_max;

while

(fabs

(m1 - m0)

>

1e-10)}

free

(v);

destroy_matrix

(&t)

;destroy_matrix

(&r)

;return

sqrt

(m1);}

double

max(

double

*v,int col)

return max;

}void

mul(

struct matrix *r,

double

*v)}

for(i =

0; i < r->col; i++

) v[i]

= tmp[i]

;free

(tmp)

;}

complex.h

#pragma once

struct complex_number

;void

add_complex_number

(struct complex_number m,

struct complex_number n)

;void

sub_complex_number

(struct complex_number m,

struct complex_number n)

;void

mul_complex_number

(struct complex_number m,

struct complex_number n)

;void

div_complex_number

(struct complex_number m,

struct complex_number n)

;void

modulus_of_complex_number

(struct complex_number m)

;

complex.cpp

#include

"complex.h"

#include

#include

void

add_complex_number

(struct complex_number m,

struct complex_number n)

void

sub_complex_number

(struct complex_number m,

struct complex_number n)

void

mul_complex_number

(struct complex_number m,

struct complex_number n)

void

div_complex_number

(struct complex_number m,

struct complex_number n)

void

modulus_of_complex_number

(struct complex_number m)

main.cpp

#include

"matrix.h"

#include

"complex.h"

#include

intmain()

2 6陣列運算和矩陣運算

1 陣列和標量的運算 陣列可以和乙個標量 1x1的矩陣 進行加 減 乘 除運算,其結果將是此標量和陣列中的每乙個元素 相加 相減 相乘 相除 而經典數學中矩陣和乙個標量不能進行加 減運算,只允許矩陣和乙個標量進行乘 除運算,並進行相除運算時,標量必須是除數,矩陣為被除數。2 乙個標量與乙個陣列的乘運...

重溫複數運算

實數是我們經常用到的,在高中數學中複數就已經接觸到了,複數包含實數和虛數,虛數在一般生活工作中很少接觸,以至於現在虛數是什麼定義都有些模糊,都無情的還給了高中數學老師。今天無意間用到了虛數,學習一下。我們把形如z a bi a,b均為實數 的數稱為複數,其中a稱為實部,b稱為虛部,i稱為虛數單位。當...

MATLAB數值運算,矩陣運算

按線性代數的要求來做 加減要各個維度相等 同型矩陣 相乘要保證前矩陣的行維數 後矩陣的列維數。ns 乘 sm a b a inv b 倒數相當於逆,可以不是同型矩陣,如齊次線性方程求解 a b inv a b 左右除法不一樣 乘方運算 a b b為正整數時,表示a矩陣自乘b次 b為負整數時,可以先將...