7Operators

Last-modified: 2025-04-17 (木) 15:10:08

7 Operators

  1. Introduction to operators
  2. Arithmetic operators
  3. Relational operators
  4. Logical operators
  5. Operators for Equations
  6. Assignment operators
  7. User defined operators

7.1 Introduction to operators

指定された優先順位で新しい演算子を定義したり、既存の演算子を定義し解除したり、既存の演算子の優先順位を再定義したりできます。演算子は、単項プレフィックスまたは単項接尾辞、バイナリ中置、n項中置、matchfix、または nofix です。「マッチフィックス」とは、引数を囲む記号のペアを意味し、「nofix」は引数を取らない演算子を意味します。さまざまなタイプの演算子の例としては、次のものがあります。

unary prefix
negation - a
unary postfix
factorial a!
binary infix
exponentiation a^b
n-ary infix
addition a + b
matchfix
list construction [a, b]

(組み込みの nofix 演算子はありません。このような演算子の例については、nofix を参照してください。.)

新しい演算子を定義するメカニズムは簡単です。関数を演算子として宣言するだけで済みます。演算子関数は、定義されている場合と定義されていない場合があります。

ユーザー定義演算子の例を次に示します。明示的な関数呼び出し "dd" (a) は dd a と同等であり、同様に "<-" (a, b) は a <- b と同等であることに注意してください。また、この例では関数 "dd" と "<-" が未定義であることにも注意してください。

(%i1) prefix ("dd");
(%o1)                          dd
(%i2) dd a;
(%o2)                         dd a
(%i3) "dd" (a);
(%o3)                         dd a
(%i4) infix ("<-");
(%o4)                          <-
(%i5) a <- dd b;
(%o5)                      a <- dd b
(%i6) "<-" (a, "dd" (b));
(%o6)                      a <- dd b

新しい演算子を定義する Maxima 関数を次の表にまとめ、デフォルトの左と右の拘束力 (それぞれ lbp と rbp) を示します。(拘束力によって演算子の優先順位が決まります。ただし、左右の拘束力は異なる可能性があるため、拘束力は優先権よりもやや複雑です。一部の演算定義関数は、追加の引数を取ります。詳細については、関数の説明を参照してください。

prefix
rbp=180
postfix
lbp=180
infix
lbp=180, rbp=180
nary
lbp=180, rbp=180
matchfix
(binding power not applicable)
nofix
(binding power not applicable)

比較のために、いくつかの組み込み演算子と、それらの左右のバインド権限を次に示します。

Operator lbp rbp

 :        180     20
 ::       180     20
 :=       180     20
 ::=      180     20
 !        160
 !!       160
 ^        140     139
 .        130     129
 *        120
 /        120     120
 +        100     100
 -        100     134
 =        80      80
 #        80      80
 >        80      80
 >=       80      80
 <        80      80
 <=       80      80
 not              70
 and      65
 or       60
 ,        10
 $        -1
 ;        -1

removeとkill AtomからRemoveオペレーターのプロパティ。remove ("a", op) はa の演算子プロパティのみを削除します。kill ("a") は、演算子のプロパティを含む a のすべてのプロパティを削除します。演算子の名前は引用符で囲む必要があることに注意してください。

(%i1) infix ("##");
(%o1)                          ##
(%i2) "##" (a, b) := a^b;
                                     b
(%o2)                     a ## b := a
(%i3) 5 ## 3;
(%o3)                          125
(%i4) remove ("##", op);
(%o4)                         done
(%i5) 5 ## 3;
Incorrect syntax: # is not a prefix operator
5 ##
 ^
(%i5) "##" (5, 3);
(%o5)                          125
(%i6) infix ("##");
(%o6)                          ##
(%i7) 5 ## 3;
(%o7)                          125
(%i8) kill ("##");
(%o8)                         done
(%i9) 5 ## 3;
Incorrect syntax: # is not a prefix operator
5 ##
 ^
(%i9) "##" (5, 3);
(%o9)                       ##(5, 3)
Categories: Operators &#183; Syntax &#183;