简体   繁体   中英

Primitive type Boolean in Maxima

I am very new in Maxima, but I know Java. I need to write a Maxima function which is written like this in Java:

private boolean isEchelon() {
    for (int i = 0; i < headElementColumnIndexes.length; i++) {
        int current = headElementColumnIndexes[i];
        for (int j = i + 1; j < headElementColumnIndexes.length; j++)
            if (current == headElementColumnIndexes[j])
                return false;
    }
    return true;
}

Is it even possible to write a function which returns a Boolean in Maxima? Can I ask for some examples?

Yes, it is possible. Maxima built in Boolean functions are called predicates, and (usually) end in "p". Examples:

(%i1) integerp (0);
(%o1)               true
(%i2) integerp (%pi);
(%o2)               false

You can write your own predicates as well.

(%i1) even_prime(n) := evenp(n) and primep(n);
(%o1)             even_prime(n) := evenp(n) and primep(n)
(%i2) even_prime(3);
(%o2)                              false
(%i3) even_prime(2);
(%o3)                              true

It may be easier at first to explicitly return the Boolean values, since this is closer to java syntax.

(%i1) even_prime(n) := if evenp(n) and primep(n) then true else false;
(%o1)      even_prime(n) := if evenp(n) and primep(n) then true else false;
(%i2) even_prime(4);
(%o2)                              false

A peculiarity of Maxima is that binary numerical relations ( = , < ,etc.) do not evaluate to a Boolean.

(%i1) 4 = 5;
(%o1)                              4 = 5
(%i2) 5 > 3;
(%o2)                              5 > 3

To force such relations to evaluate to a Boolean, use the is function.

(%i1) is (4 = 5);
(%o1)                              false
(%i2) is (5 > 3);
(%o2)                              true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM