利用AMPL实现矩阵的加、乘和求逆

上代码吧,看得很清楚哟。意思不太大,就当是熟悉一下AMPL编程吧。

param N:=3;
param A{1..N,1..N};
param B{1..N,1..N};
var Ainv{1..N,1..N};
data;
param A :=
    : 1 2 3 :=
    1 1 4 7
    2 2 5 8
    3 3 6 1 ;
let{i in 1..N, j in 1..N}  B[i,j]:=A[i,j]+A[i,j];
display B;
let{i in 1..N, j in 1..N}  B[i,j]:=sum{k in 1..N} A[i,k]*A[k,j];
display B;
C1{i in 1..N, j in 1..N}:
  sum{k in 1..N} A[i,k]*Ainv[k,j] =if i=j then 1;
solve;
display Ainv;


你可能感兴趣的:(编程)