跳轉到內容

Maxima/資料結構

來自華夏公益教科書,為開放世界提供開放書籍

"我必須說,Maxima 當前的陣列/矩陣語義很混亂,至少有四種不同型別的物件(雜湊陣列、顯式列表、顯式矩陣和 Lisp 陣列)支援帶有不同語義的下標。" [1]

"Maxima 的陣列、列表和矩陣的概念非常混亂,因為在該專案的多年發展過程中,各種想法逐漸累積...是的,這確實很亂。對此我表示歉意。這些都是有趣的想法,但沒有統一的框架。" [2]

Maxima 有兩種陣列型別:[3]

  • 未宣告陣列(以 Lisp 雜湊表實現)
  • 已宣告陣列(以 Lisp 陣列實現)
    • 由 array 函式建立
    • 由 make_array 函式建立。


類別:陣列

  • array
  • arrayapply
  • arrayinfo
  • arraymake
  • arrays - 全域性變數
  • fillarray
  • listarray
  • make_array
  • rearray
  • remarray
  • subvar
  • use_fast_arrays

另請參閱

  • 由 := 和 define 定義的陣列函式。


未宣告陣列

[編輯 | 編輯原始碼]

賦值會建立一個未宣告陣列。

(%i1) c[99] : 789;
(%o1)                          789
(%i2) c[99];
(%o2)                          789
(%i3) c;
(%o3)                           c
(%i4) arrayinfo (c);
(%o4)                   [hashed, 1, [99]]
(%i5) listarray (c);
(%o5)                         [789]


"如果使用者在宣告相應的陣列之前對下標變數賦值,則會建立一個未宣告陣列。未宣告陣列,也稱為雜湊陣列(因為對下標進行雜湊編碼),比已宣告陣列更通用。使用者不需要宣告其最大大小,並且隨著分配更多元素的值,它們會透過雜湊動態增長。未宣告陣列的下標甚至不需要是數字。但是,除非陣列非常稀疏,否則在可能的情況下宣告它可能比不宣告它更有效。"(來自 Maxima CAS 文件)

"如果執行以下操作,則會建立:"

b[x+1]:y^2 

"(並且 b 尚未是陣列、列表或矩陣 - 如果它是其中之一,則會導致錯誤,因為 x+1 對於 art-q 陣列、列表或矩陣而言不是有效的下標)。

它的索引(也稱為鍵)可以是任何物件。它一次只接受一個鍵(b[x+1,u]:y 會忽略 u)。引用是透過 b[x+1] ==> y^2 完成的。當然,鍵可以是列表,例如

 b[[x+1,u]]:y 

是有效的。"(來自 Maxima CAS 文件)

已宣告陣列

[編輯 | 編輯原始碼]
  • 宣告陣列(給出名稱並分配記憶體)
  • 填充陣列
  • 處理陣列


array 函式

[編輯 | 編輯原始碼]
Function: array (name, type, dim_1, …, dim_n)

建立一個 n 維陣列。這裡

  • type 可以是 fixnum(用於有限大小的整數)或 flonum(用於浮點數)
  • n 可以小於或等於 5。第 i 維的下標是從小到大執行的整數,從 0 到 dim_i。
(%i5) array(A,2,2);
(%o5)                                  A
(%i8) arrayinfo(A);
(%o8)                      [declared, 2, [2,2]]

因為陣列 A 元素的下標從 0 到 dim,所以陣列 A 有

( dim_1 + 1) * (dim_2 + 1) = 3*3 = 9 

個元素。


array 函式可以用於將未宣告陣列轉換為已宣告陣列。

arraymake 函式

[編輯 | 編輯原始碼]
Function: arraymake (A, [i_1, …, i_n])

結果是一個未計算的陣列引用。

(%i12) arraymake(A,[3,3,3]);
(%o12)                    array_make(3, 3, 3)
                                           3, 3, 3
(%i13) arrayinfo(A);
       arrayinfo: A is not an array.

函式:make_array

[編輯 | 編輯原始碼]
Function: make_array (type, dim_1, …, dim_n)

建立並返回一個 Lisp 陣列。

Type 可以是

  • 任何
  • flonum
  • fixnum
  • hashed
  • functional。

有 n 個索引,第 i 個索引從小到大執行,從 0 到 dim_i - 1。

make_array 相對於 array 的優勢在於,返回值沒有名稱,並且一旦指向它的指標消失,它也會消失。例如,如果 y: make_array (...),那麼 y 指向一個佔用空間的物件,但在 y: false 之後,y 不再指向該物件,因此該物件可以被垃圾回收。

示例

(%i9) A : array_make(3,3,3);
(%o9)                         array_make(3, 3, 3)
(%i10) arrayinfo(A);
(%o10)                     [declared, 3, [3, 3, 3]]

對列表元素的賦值。

(%i1) b : [1, 2, 3];
(%o1)                       [1, 2, 3]
(%i2) b[3] : 456;
(%o2)                          456
(%i3) b;
(%o3)                      [1, 2, 456]


(%i1) mylist : [[a,b],[c,d]];
(%o1) [[a, b], [c, d]]

由 Burkhard Bunk 提供的有趣示例:[4]

lst: [el1, el2, ...];         contruct list explicitly
lst[2];                       reference to element by index starting from 1
cons(expr, alist);            prepend expr to alist
endcons(expr, alist);         append expr to alist
append(list1, list2, ..);     merge lists
makelist(expr, i, i1, i2);    create list with control variable i
makelist(expr, x, xlist);     create list with x from another list     
length(alist);                returns #elements
map(fct, list);               evaluate a function of one argument
map(fct, list1, list2);       evaluate a function of two arguments

類別:列表

  • [
  • ]
  • append
  • assoc
  • cons
  • copylist
  • create_list
  • delete
  • eighth
  • endcons
  • fifth
  • first
  • flatten
  • fourth
  • fullsetify
  • join
  • last
  • length
  • listarith
  • listp
  • lmax
  • lmin
  • lreduce
  • makelist
  • member
  • ninth
  • permut
  • permutations
  • pop
  • push
  • random_permutation
  • rest
  • reverse
  • rreduce
  • second
  • setify
  • seventh
  • sixth
  • some
  • sort
  • sublist
  • sublist_indices
  • tenth
  • third
  • tree_reduce
  • xreduce
/* example by Burton Willis */
(%i28) P1(s) := rectform(product(s[i],i, 1, length(s)))$

(%i29) P2(s) := xreduce(lambda([a,b], rectform(a*b)),s)$

(%i30) s : makelist(random(1.0)+%i*random(1.0),10^4)$

(%i31) P1(s);
Evaluation took 32.3080 seconds (32.3860 elapsed) using 17944.301 MB.
(%o31) 0.0

(%i32) P2(s);
Evaluation took 0.0620 seconds (0.0620 elapsed) using 8.343 MB.
(%o32) 0.0

"在 Maxima 中,矩陣是以巢狀列表(即矩陣的行列表)實現的"。例如:[5]

(($MATRIX) ((MLIST) 1 2 3) ((MLIST) 4 5 6))

從巢狀列表構建矩陣

(%i3) M:matrix([1,2,3],[4,5,6],[0,-1,-2]);
                                [ 1   2    3  ]
                                [             ]
(%o3)                           [ 4   5    6  ]
                                [             ]
                                [ 0  - 1  - 2 ]


由 Burkhard Bunk 提供的有趣示例:[6]

A: matrix([a, b, c], [d, e, f], [g, h, i]);     /* (3x3) matrix */
u: matrix([x, y, z]);                           /* row vector */
v: transpose(matrix([r, s, t]));                /* column vector */

對元素的引用等

u[1,2];                 /* second element of u */
v[2,1];                 /* second element of v */
A[2,3];   or  A[2][3];  /* (2,3) element */
A[2];                         /* second row of A */
transpose(transpose(A)[2]);   /* second column of A */

逐元素運算

A + B;     
A - B;
A * B;      
A / B;
A ^ s;      
s ^ A;

矩陣運算

A . B;                  /* matrix multiplication */
A ^^ s;                 /* matrix exponentiation (including inverse) */
transpose(A);
determinant(A);
invert(A);




類別:矩陣:[7]

  • addcol
  • addrow
  • adjoint
  • augcoefmatrix
  • cauchy_matrix
  • charpoly
  • coefmatrix
  • col
  • columnvector
  • covect
  • copymatrix
  • determinant
  • detout
  • diag
  • diagmatrix
  • doallmxops
  • domxexpt
  • domxmxops
  • domxnctimes
  • doscmxops
  • doscmxplus
  • echelon
  • eigen
  • ematrix
  • entermatrix
  • genmatrix
  • ident
  • invert
  • list_matrix_entries
  • lmxchar
  • matrix
  • matrix_element_add
  • matrix_element_mult
  • matrix_element_transpose
  • matrixmap
  • matrixp
  • mattrace
  • minor
  • ncharpoly
  • newdet
  • nonscalar
  • nonscalarp
  • permanent
  • rank
  • ratmx
  • row
  • scalarmatrixp
  • scalarp
  • setelmx
  • sparse
  • submatrix
  • tracematrix
  • transpose
  • triangularize
  • zeromatrix

函式:matrix

[編輯 | 編輯原始碼]
Function: matrix (row_1, …, row_n)

返回一個矩形矩陣,該矩陣具有行 row_1、…、row_n。每行都是表示式列表。所有行必須具有相同的長度。

. 運算子

[編輯 | 編輯原始碼]

"點運算子,用於矩陣(非交換)乘法。當 "." 以這種方式使用時,應該在兩側留出空格,例如

A . B

這清楚地區分了它與浮點數中的小數點。

. 運算子表示非交換乘法和標量積。當運算元是 1 列或 1 行矩陣 a 和 b 時,表示式 a.b 等效於 sum (a[i]*b[i], i, 1, length(a))。如果 a 和 b 不是複數,則這是 a 和 b 的標量積,也稱為內積或點積。標量積定義為 conjugate(a).b,其中 a 和 b 是複數;eigen 包中的 innerproduct 提供複數標量積。

當運算元是更一般的矩陣時,乘積是矩陣乘積 a 和 b。b 的行數必須等於 a 的列數,結果的行數等於 a 的行數,列數等於 b 的列數。

為了將 . 作為算術運算子與浮點數中的小數點區分開來,可能需要在兩邊留出空格。例如,5.e3 為 5000.0,而 5 . e3 為 5 乘以 e3。

有幾個標誌控制著涉及 . 的表示式的簡化,它們分別是 dot0nscsimp、dot0simp、dot1simp、dotassoc、dotconstrules、dotdistrib、dotexptsimp、dotident 和 dotscrules。(來自 Maxima CAS 文件)

類別:向量 

  • 向量
  • eigen
  • 表示式
  • nonscalar
  • nonscalarp
  • scalarp

類別:包 vect[8]

  • vect
  • 縮放因子
  • vect_cross
  • vectorpotential
  • vectorsimp
  1. Maxima:Stavros Macrakis 的語法改進
  2. Maxima:Maxima 將“陣列”稱為什麼? - 由 Robert Dodier 回答
  3. Maxima 郵件列表: [Maxima] 陣列和矩陣? - 由 Robert Dodier 回答
  4. Burkhard Bunk 的 Maxima 概述
  5. Maxima 郵件列表: [Maxima] 陣列和矩陣? - 由 Robert Dodier 回答
  6. Burkhard Bunk 的 Maxima 概述
  7. Maxima 手冊:類別矩陣
  8. Maxima 手冊:包 vect
華夏公益教科書