書式
nterms(式)
で用い、「式」の項の個数を返す。
(%i1) x^2 + x + 1;
2
(%o1) x + x + 1
(%i2) nterms(%);
(%o2) 3
「式」は展開され、しかし簡易化は行われずに項の個数がカウントされる。次の例を参照:
(%i3) f: (x - 1)^2 + (x + 1)^2;
2 2
(%o3) (x + 1) + (x - 1)
(%i4) nterms(%);
(%o4) 6
(%i5) expand(f);
2
(%o5) 2 x + 2
(%i6) nterms(%);
(%o6) 2
(x+1)2 と (x-1)2 を展開するとそれぞれ x2 + 2x + 1、x2 - 2x + 1 となるから項数は 3 + 3 = 6 となる。ただし、三角関数などは展開されずカウントされる。
(%i7) g: sin(x - y); (%o7) - sin(y - x) (%i8) nterms(%); (%o8) 1 (%i9) trigexpand(g); (%o9) sin(x) cos(y) - cos(x) sin(y) (%i10) nterms(%); (%o10) 2
なお、方程式に適用すると 1 を返す。
(%i11) x^2 - x - 1 = 0;
2
(%o11) x - x - 1 = 0
(%i12) nterms(%);
(%o12) 1