Maple 11 Introductory Programming Guide

William Fish's picture

On page 121 of the 2007 edition of the Maple 11 Introductory Programming Guide in section 3.6 Exercises, problem 3, it says:

"... Find the inverse of (a*b)/c+((a-d)/(b*e))*I) in standard form, where a, b, c, d and e are real."

Here is my failed attempt:

View 4937_Problem_3.mw on MapleNet or Download 4937_Problem_3.mw
View file details

TIA

Scott03's picture

Input

The question is asking for you to enter in (a*b)/c+((a-d)/(b*e))*I), the book had an extra bracket, but you entered the following in your worksheet(a*b)/(c+((a-d)/(b*e)*I)).

Scott

matched delimiters

Scott, the question is asking about (a*b)/c+((a-d)/(b*e))*I), which has unmatched parentheses. Hence there is a typo in the book. William Fish was just taking a reasonable guess at where the typo was.

William, to find the inverse in standard form, try this.
(a*b)/(c+((a-d)/(b*e)*I));
evalc(1/%);

William Fish's picture

evalc

D. J. Keenan:

That did the trick. Thank you.

Pages 69 and 70 of the Guide should have but seem not to have made an impression when I first read them. Thanks again.

I haven't been able to get The Assume Facility to help. I tried it on an integers only problem a while back and it didn't help there either.

William Fish's picture

De Morgan's laws

Problem 7.

Something is happening in the following that I don't understand.

View 4937_Problem_7.mw on MapleNet or Download 4937_Problem_7.mw
View

Can anybody enlighten me?

Robert Israel's picture

Precedence

The logical operators (not, and, or) all have lower binding strength than =. This means that your

not(P or Q) = (not P) and (not Q) 

is actually parsed as

not((P or Q) = (not P)) and (not Q)

.
Now not((P or Q) = (not P)) evaluates to true (because
(P or Q) is not the same thing as (not P)), so the result is
(not Q). In order to get what you want, you need more
parentheses:

> (not(P and Q)) = ((not P) or (not Q));

(not (P and Q)) = (not (P and Q))

> (not(P or Q)) = ((not P) and (not Q));

(not (P and Q)) = (not (P and Q))
However, Maple's simplification of expressions involving these operators is not always complete. For example:

> (not(P and not Q)) = ((not P) or Q);

(not (P and not Q)) = (not P or Q)

If you want to do boolean logic with Maple, I think it's better to use the Logic package.

Doug Meade's picture

prettyprint is pretty but unclear

Just to clarify something in Robert's posting. The apparent discrepancy in the last displayed output in the previous posting only appears to be incorrect. The problem is that Maple's prettyprinter has decided to omit a level of parentheses that we humans would generally leave in our scribblings.

To be more explicit: not P or Q means (not P) or Q.

for P in [true,false,FAIL] do
  for Q in [true,false,FAIL] do
    print( (not(P and not Q)) = (not P or Q) );
  end do;
end do;
                                 true = true
                                false = false
                                 FAIL = FAIL
                                 true = true
                                 true = true
                                 true = true
                                 true = true
                                 FAIL = FAIL
                                 FAIL = FAIL

Doug

---------------------------------------------------------------------
Douglas B. Meade
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/
William Fish's picture

Logic

Robert Israel:

Thanks.

At some point in the future I hope to be less frequently surprised by Maple.

The top of page 125 of the 2007 Maple Introductory Programming Guide seems to says that using evalb(exp1 = exp2) helps.

Thanks again.

Robert Israel's picture

Logic

It may help sometimes, but it is often misleading.
evalb (A = B) returns true only when A and B are literally the same (after evaluation). It does not check for mathematical or logical equivalence. So for example,

> evalb((not(P and not Q)) = ((not P) or Q));

false

William Fish's picture

evalb

> evalb((not(P and not Q)) = ((not P) or Q));

false

Again, I am very surprised but I like this:

> for P in [true, false, FAIL] do
for Q in [true, false, FAIL] do
B := evalb(`not`(`and`(P, `not`(Q))) = `or`(`not`(P), Q));
print(B)
end do;
end do;
true
true
true
true
true
true
true
true
true

William Fish's picture

Whitespace

Doug Meade:

Why is it that your comment looks so much better than mine even though I tried to copy yours? How did you get your whitespace past the whitespace police?

Doug Meade's picture

use the <pre> format tag

William,

I always enclose the Maple commands, and output, in a <pre> tag. So, what I really entered is:

<pre>
for P in [true,false,FAIL] do
  for Q in [true,false,FAIL] do
     ...
   end do;
end do;
...
</pre>

Now, entering the above so that you could see what I actually type, was a little more of a challenge.

Note that the displayed title in the Recent Comments list shows the source for the subject.

I hope this is helpful,

Doug

---------------------------------------------------------------------
Douglas B. Meade
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/
William Fish's picture

Whitespace

Thanks. I'll try

x
 y
  z

next time.

William Fish's picture

The Sieve of Eratosthenes

On page 166 of the 2007 edition of the Maple 11 Introductory Programming Guide in section 4.9 Exercises, problem 4 it says:

"Write a procedure that implements the sieve of Eratosthenes: Count the number of integers (less than or equal to a given integer) that are prime."

The following seems to be a solution:

View 4937_Eratosthenes.mw on MapleNet or Download 4937_Eratosthenes.mw
View file details

I'd like to read other solutions.

gkokovidis's picture

help-about-sieve-of-eratosthenes

From a previous Mapleprimes thread dealing with the Sieve.

help-about-sieve-of-eratosthenes

Regards,
Georgios Kokovidis
Dräger Medical

William Fish's picture

'if' Operator

The 'if' operator doesn't seem to work for me. Here is what I get:

View 4937_if_operator_problem.mw on MapleNet or Download 4937_if_operator_problem.mw
View file details

Please tell me what I'm doing wrong.

P.S.

That ES proc from http://beta.mapleprimes.com/forum/help-about-sieve-of-eratosthenes
is way cool and very fast. My Eratosthene proc doesn't compile.

Scott03's picture

Delete the quotes

Delete the ' quotes around the if statement and all should be well. Well at least I found it worked after that when running on my Maple 11.01.

Scott

back quotes

When using this version of the if command in worksheet mode (1D input), back quotes - also known as left quotes - are needed as shown on the help page.

Hope this helps.

J. Tarr

William Fish's picture

Apostrophe

Sorry, I seem to have used the wrong single quote, apostrophe. I though that I tried grave accent and got an error in the chaos, something about trampling on a reserved word. The grave accents are now working as shown in the book.

back quote

"`" ASCII code 96. Common names: left quote; left single quote; open quote; ITU-T: grave accent; grave. Rare: backprime; INTERCAL: backspark; unapostrophe; birk; blugle; back tick; back glitch; push; ITU-T: opening single quotation mark; quasiquote.

William Fish's picture

Exercise 5 of Section 5.5

I would like to see a solution to exercise 5 on page 191 of the 2007 Maple Introductory Programming Guide. The exercise reads as follows:

"Write a procedure,SPLIT, that, upon input of a product f and a variable x, returns a list of two values. The first item in the list should be the product of the factors in f that are independent of x, and the second item should be the product of the factors that contain an x.

Hint: Use the has, select, remove, and selectremove commands."

I'm not even sure what to use as input should I manage to come up with a proc to test.

If you wish to be confused see my attempt in the following:

View 4937_Help.mw on MapleNet or Download 4937_Help.mw
View file details

Robert Israel's picture

Solution

It's really pretty simple...

> SPLIT:= (f,x) -> [selectremove](`not` @ has, f, x);
JacquesC's picture

Another solution

I dislike has because it gives false positives. I would rather write
SPLIT := (f,x) -> [selectremove](type, f, 'freeof'(x));

This will then work properly on products containing objects like Int(g(x),x=0..t) which do not 'depend' on x at all.

William Fish's picture

Exercises

Are solutions for the exercises in the 2007 Maple Introductory Programming Guide available?

William Fish's picture

Exercise 5 Solutions

Robert Israel and Jacques C.:

Thank you for the solutions to exercise 5.

I have never seen "[selectremove]". I don't know what it means. I tried Maple Help but it was no help. I tried Google but all it would look for is "selectremove". I've never used "@". I have read about it. I've never successfully used "has" and "freeof" is new to me.

This technology is sufficiently beyond me that I can not distinguish it from magic.

Can you help me get to the point where I might be able to apply this technology?

I don't think that I have a good enough understanding of the question (exercise 5).

The input "int(g(x), x = 0 .. t),x" is helpful. I think other examples of appropriate SPLIT input would be helpful.

Thanks again.

selectremove and freeof

Please see ?selectremove third para and example 2.12. The square brackets around it ensure the results appear as a list. Also, please see ?type/freeof.

Hope these help pages make things clearer.

J. Tarr

William Fish's picture

[selectremove](`not` @ has, f, x)

I see, I see, I see more but not all.

"The square brackets around "selectremove" ensure the results appear as a list."

I would have never thought of that. I would have put it all in square brakets and that also seems to work:
SPLIT:=(f,x)->[selectremove(`not`@has,f,x)]
as does:
SPLIT:=[(f,x)->selectremove(`not`@has,f,x)]
which seems to be the same as:
SPLIT:=[proc(f, x)selectremove(`@`(`not`, has), f, x) end proc]

Where to gainfully employ the grave accents (left quotes) is still a mystery to me.

The `@` and `not` working on "has" is wonderful but I don't think I would ever have thought of it without help.

If I were asked for the answer to "SPLITS(int(g(x), x = 0 .. t), x)" I think I would have answered as follows: [int(g(x), x = 0 .. t),{}]. Everything is independent of x and nothing contains x. x is just the dummy variable of integration.

Please, explain `not`@has.

Robert Israel's picture

`not` @ has

Left quotes `` are needed around `not` because not is a reserved word. The left quotes make it into a name that can be manipulated like other functions. @ is the composition operator, i.e. f @ g is the function such that (f @ g)(x) = f(g(x)). So `not` @ has is a function: (`not` @ has)(f,x) returns true if has(f,x) returns false and vice versa. A more pedestrian way of doing the same thing would be

> SPLIT := (f,x) -> [selectremove(g -> not has(g,x), f)];

William Fish's picture

`not` @

Robert Israel:

Did "`not` @ Boolean-valued procedure" fly out of your typing finders on the spur of the moment or had you seen it before somewhere?

Thank you for your help.

Doug Meade's picture

Maple is evolutionary

William,

I'll let Robert speak for himself.

Personally, my Maple knowledge is very evolutionary. I started with some basic skills and techniques. Then, by following various online discussions, my knowledge increased.

The Maple language has also evolved. While great pains have been taken to attempt to preserve backward compatibility, you will sometimes see reference to a solution that works only with a sufficiently new version of Maple.

It is entirely possible that the solution presented by Robert would not work in an older version of Maple.

I would not expect the vast majority of Maple users to come up with Robert's solution on their first attempt. Anyone who does, has done something similar in the past and has developed an ability to think in terms of functions. This can be very powerful, but is not always the most natural for the general user.

There are lots of tricks like putting the square brackets around the command name. Whether you like these or not depends on your background. I know many users are completely baffled by this and find code written in this way to be unreadable (at their experience level). There are issues of performance, but for most users these are overwhelmed by issues of ease of understanding and the amount of time available to develop their solution.

This is already longer than I intended it to be. Consider it as one user's opinion. Others can chime in with their personal opinions.

Doug

---------------------------------------------------------------------
Douglas B. Meade
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/
Robert Israel's picture

flying out

Nothing flies out by itself. Yes, I have seen this sort of construction on many occasions. I agree with Doug's comments. I was a Maple novice about 13 years ago, and have learned a lot since then, especially from forums such as this one (by asking questions, by reading other people's answers, and by attempting to answer questions myself).

functional forms of inline operators

It isn't clear which part suprised the original poster: the backquotes around the not or the use of the ampersand. The latter is standard technique for function composition; it isn't the most efficient way to do this, so you won't see it much if you look at Maple library code, but it is a convenience when calculating with Maple.

The backquoted not is a different animal altogether. As mentioned earlier in this thread, the backquotes are necessary because not is a Maple keyword (see ?keyword). However, that only explains why `not` doesn't generate a syntax error. It works because the name `not` is assigned a builtin procedure that negates a boolean. Most (if not all) Maple keywords that correspond to inline operators have equivalent functional forms:

a and b       = `and`(a,b)
a implies b   = `implies`(a,b)
a in b        = `in`(a,b)
a intersect b = `intersect(a,b)
a minus b     = `minus`(a,b)
a mod b       = `mod`(a,b)  # almost.  `mod` is actually assigned modp or mods...
not a         = `not`(a)
a or b        = `or`(a,b)
a subset b    = `subset`(a,b)
a union b     = `union`(a,b)   # can take multiple arguments
a xor b       = `xor`(a,b) 

Note that the equivalent keywords done, stop, and quit also have corresponding functions. These can be used with command-line maple to return an error code and, unlike the keywords, can be called from inside procedures.

JacquesC's picture

It gets better

There are some little known (and even less documented) functional versions of other parts of the syntax. My favourites being `[]` and `{}` for the list and set constructors, respectively.

There is also `||` for concatenation and `..` for ranges.

But the ones that I really want to use [but keep forgetting about] are `?[]` for constructing a tableref, and `?()` as an almost-synonym for apply. It is the possibility of using `?[]` in a map or map2 that really starts to make things really interesting!

Not to forget `<,>` and `<|>` which are the names that the LinearAlgebra package uses for the syntactic shorthands for entering Matrix/Vector expressions.

no identity

Missing, alas, is an identity function. I've suggested assigning `()` for this, since it is equivalent to a sequence constructor. That is, `()`(a,b,c) would return the sequence a,b,c.

William Fish's picture

Maple Programming

"Literate programs are written to be read by other software developers." — Donald Knuth

But, I'm not developing software, I just want to lean how to get Maple to assist me as I try to solve problems, look for answers, truth, the meaning of life and the American way...

"The Feynman Problem-Solving Algorithm:
  (1) write down the problem;
  (2) think very hard;
  (3) write down the answer."

This stuff is cool:

> SPLIT:= (f,x) -> [selectremove](`not` @ has, f, x);

and I find it entertaining, better than Circ De Sola.

"In most computer programming languages, the challenge might be to solve the puzzle in the fewest lines of code. In APL, it is generally a given that the solution will be one line of code: The challenge is to solve the problem in the fewest characters!"

"There are two kinds of geniuses: the 'ordinary' and the 'magicians'. An ordinary genius is a fellow whom you and I would be just as good as, if we were only many times better. There is no mystery as to how his mind works. Once we understand what they've done, we feel certain that we, too, could have done it. It is different with the magicians. Even after we understand what they have done it is completely dark. Richard Feynman is a magician of the highest calibre." — Mark Kac

What if Feynman was born in 1979 with Maple? Are there Feynmans out there now?

"... as far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." — Albert Einstein

Math can be fun and sometimes it is even helpful in the real world.

William Fish's picture

Pi

On page 252 of the 2007 edition of the Maple 11 Introductory Programming Guide:

> Max := proc (x::numeric, y::numeric)
            if y < x then
              x
            else
              y
            end if
  end proc:
> Max(Pi, 3);

Error, invalid input: Max expects its 1st argument, x, to be of type numeric, but received Pi

Why is this a good thing? Why isn't Pi numeric?

Same Problem

We've run into the same problem recently. We don't know why, but < does not seem to want to compare numbers (integers, rational numbers, or floats) to what I'll call "numeric symbols" such as Pi, exp(nonzero integer), sin(Pi/7), etc.

What it comes down to is that evalb() has great weaknesses when dealing with inequalities. We've switched to using verify() for our inequalities.

[> verify( Pi, 3, 'less_than' );
false

However, we've noticed that verify() has surprising weaknesses when it comes to dealing with infinity!

[> verify( infinity, infinity );
true
[> verify( infinity, infinity, 'simplify' );
false
[> verify( -infinity..sqrt(35), -infinity..sqrt(5)*sqrt(7), 'range' );
false
[> verify( -infinity..sqrt(35), -infinity..sqrt(5)*sqrt(7), 'range'('simplify') );
false

--Schivnorr

more Pi

The help page type/numeric defines a numeric type:

  - The type(x, numeric) function returns true if x is an integer, fraction, or
    finite floating-point number (float).

Pi is none of those, hence not of type numeric. However, it is of type realcons, so you could change the type specification in your Max procedure to realcons and avoid the type matching error. However, that doesn't solve your problem:

  if Pi < 3 then yes else no end if;
Error, cannot determine if this expression is true or false: Pi < 3

You could use evalf in the conditional to evaluate the relation, however, doing so can introduce floating-point round-off issues. The builtin function max handles that by using evalr, which uses range arithmetic.

Robert Israel's picture

evalr

Actually evalr doesn't prevent round-off issues, because it just does its floating-point computations with the current Digits. For example:

evalr(INTERVAL(0..1.0)/3.0);

INTERVAL(0. .. 0.3333333333)

William Fish's picture

Section 6.12, Exercise 1

The following worksheet contains a solution to exercise 1 on page 255 of the 2007 edition of the Maple 11 Introductory Programming Guide. It is wrong for only one of the test cases included.

I tried to use a sequence for a while but ended up using a list, l. I would find a sequence solution interesting. I couldn't do it.

View 4937_Exercises10.mw on MapleNet or Download 4937_Exercises10.mw
View file details

Robert Israel's picture

Improving Max

Here's one way I might do it.

> Max:= proc()
  local symargs, numargs, m;
  numargs, symargs:= selectremove(type,[args],realcons);
  if nops(numargs) > 0 then
    m:= -infinity;
    for t in numargs do 
       if is(t > m) then m:= t fi
       od;
    if nops(symargs) = 0 then m
    else 'procname'(m,op(symargs))
    fi
  else 'procname'(op(symargs))
  fi
 end proc;

how so?

How is that different from max? It also eliminates all but the largest realcons.

Robert Israel's picture

Point of the exercise

I think the point of the exercise was to do it without "max".

JacquesC's picture

Usage of is

Raw use of 'is' is not recommended. It is safer (and much more powerful) to replace is(t>m) with signum(t-m)=1. signum actually calls is multiple times with different questions which can help resolve the problem.

Usage of signum

I think that is something that should be changed in is. In any case, though, monadic signum should generally be avoided, because signum(0) is commonly unpredictable; use signum(0,x,y), perhaps.

William Fish's picture

One way I wouldn't have thought of

Robert Israel,

I like it:

Max:=proc()
   local symargs,numargs,m,t;                                            
   numargs,symargs:=selectremove(type,[args],realcons);
   if nops(numargs)>0 then
      m:=-infinity;
      for t in numargs do
         if is(t>m) then m:=t fi
      od;
   if nops(symargs)=0 then m
   else 'procname'(m,op(symargs))
   fi;
end proc;

Joe Riel mentioned realcons but I didn't get how to use it. Now I see I think.

I think that this is the first time I've seen a sequence on the lhs of ":=".

This is the first time I've seen "fi" and "od". Help says almost nothing about them.

I still don't understand why "is(t>m)" differs from "t>m" but I tried "t>m" and there were errors with Pi and e.

What does "Raw use of 'is' is not recommended..." mean?

Finally, it works in all cases and it's impressively short.

View 4937_Exercises10.mw on MapleNet or

Some answers

"fi" and "od" are equivalent to "end if" and "end do", respectively.

is(t>m) returns a boolean value, whereas t>m is just an expression. Whenever you say "if (something) then" Maple will call evalb() on the something. As I mentioned before, evalb(t>m) has major problems when t or m is of type realcons and not type numeric. evalb() has no troubles with boolean expressions.

Jacques was saying that simply using is(t>m) is not recommended because of the behaviour of is().

William Fish's picture

interface(verboseproc = 2); print(max)

I did this:

interface(verboseproc = 2); print(max)

and got this:

proc () option builtin = max; end proc

others (Joe Riel) seem to have seen more. How can I see more?

View 4937_Exercises10.mw on MapleNet or Download 4937_Exercises10.mw
View file details

printlevel

No, I didn't see any more. With a builtin function, which max is, that is usually all you are going to see. I did mention that max calls evalr. You can see that by doing

restart;
printlevel := 30:
max(Pi,3);

Assign printlevel a larger integer to see more details. See ?printlevel.

William Fish's picture

Chapter 6, Exercise 2

The following worksheet contains a solution to exercise 2 on page 255 of the 2007 edition of the Maple 11 Introductory Programming Guide. The exercise is to implement the given function first as a procedure and then by the mapping notation, evaluate each at 1/2 and 0.5 and comment on the different results. I did this:

View 4937_Chapter6Exercise2.mw on MapleNet or Download 4937_Chapter6Exercise2.mw
View file details

I know that I have much to learn and I know that you can teach me things that I will find wondrous.

I tried a polar plot and I think it's not working.

looks fine to me

Why do you think it isn't working? It looks correct to me. You might change the axes to boxed so that you can see the path from the origin to the point (-1,0). Here's the same plot using Re and Im:

plot([(Re,Im)(F(t)),t=0..2], axes=boxed);

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}