Post Reply 
Problem with a list in CAS-program
10-11-2017, 09:45 AM (This post was last modified: 10-11-2017 01:56 PM by Arno K.)
Post: #1
Problem with a list in CAS-program
Is something like l(K):=l(K)+1 forbidden in a CAS-program? The list seems to be killed when the program runs through this order:
Code:
#cas
POSDIVNF(n):=
BEGIN
LOCAL l,p,j,m;
l:= MAKELIST(1,X,1,n);
PRINT();
 FOR j FROM 2 TO n DO
  p:=1;
m:=j;
   WHILE p≤j DO
     p:=nextprime(p);
      WHILE irem(m,p)==0 DO
      m:=iquo(m,p);
K:=p;//only inserted K as I had to break down the error by hand, lack of debug
PRINT(K);
PRINT(SIZE(l));
l(K):=l(K)+1;
//PRINT(l);
      END;
   END;
 END;

  return ΠLIST(l);
END;
#end
the same code runs fine as program for home, the cas-program prints 2,8,3,3...
, notice the size of l: first 8 then 3, and returns: K->"Error:Invalid Dimension"
Arno
Find all posts by this user
Quote this message in a reply
10-11-2017, 11:46 AM (This post was last modified: 10-11-2017 11:54 AM by toml_12953.)
Post: #2
RE: Problem with a list in CAS-program
(10-11-2017 09:45 AM)Arno K Wrote:  Is something like l(K):=l(K)+1 forbidden in a CAS-program? The list seems to be killed when the program runs through this order:
Code:
#cas
POSDIVNF(n):=
BEGIN
LOCAL l,p,j,m;
l:= MAKELIST(1,X,1,n);
PRINT();
 FOR j FROM 2 TO n DO
  p:=1;
m:=j;
   WHILE p≤j DO
     p:=nextprime(p);
      WHILE irem(m,p)==0 DO
      m:=iquo(m,p);
K:=p;//only inserted K as I had to break down the error by hand, lack of debug
PRINT(K);
PRINT(SIZE(l));
l(K):=l(K)+1;
//PRINT(l);
      END;
   END;
 END;

  return ΠLIST(l1);
END;
#end
the same code runs fine as program for home, the cas-program prints 2,8,3,3...
, notice the size of l: first 8 then 3, and returns: K->"Error:Invalid Dimension"
Arno

I copied and pasted your code into a virtual calc on a PC and it works fine except it returns l1 since l1 isn't defined anywhere.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
10-11-2017, 11:58 AM
Post: #3
RE: Problem with a list in CAS-program
(10-11-2017 09:45 AM)Arno K Wrote:  Is something like l(K):=l(K)+1 forbidden in a CAS-program?

Try with l[K]:=l[K]+1
Find all posts by this user
Quote this message in a reply
10-11-2017, 01:46 PM (This post was last modified: 10-11-2017 01:50 PM by DrD.)
Post: #4
RE: Problem with a list in CAS-program
(10-11-2017 11:58 AM)Didier Lachieze
Try with l[K' Wrote:  
:=l[K]+1

It works with matrix brackets instead of list brackets, but it still seems like a bug, because, even using square brackets, the command entry shows up in history the same way Arno posed the original question.

Note: (The quote operation is interesting ... seems to be a quote artifact with the square brackets).
Find all posts by this user
Quote this message in a reply
10-11-2017, 01:47 PM (This post was last modified: 10-11-2017 01:57 PM by Arno K.)
Post: #5
RE: Problem with a list in CAS-program
(10-11-2017 11:46 AM)toml_12953 Wrote:  [quote='Arno K' pid='81326' dateline='1507715147']

I copied and pasted your code into a virtual calc on a PC and it works fine except it returns l1 since l1 isn't defined anywhere.

Well, that was not intended, I had tried various attempts and simply forgot to rechange l1 to l.
Arno
Edit: Changed that in first post
Find all posts by this user
Quote this message in a reply
10-11-2017, 01:53 PM (This post was last modified: 10-11-2017 01:54 PM by Arno K.)
Post: #6
RE: Problem with a list in CAS-program
(10-11-2017 01:46 PM)DrD Wrote:  
(10-11-2017 11:58 AM)Didier Lachieze
Try with l[K' Wrote:  
:=l[K]+1

It works with matrix brackets instead of list brackets, but it still seems like a bug, because, even using square brackets, the command entry shows up in history the same way Arno posed the original question.
Thank you, as I had to break down to eliminate several possibilities I simply forgot to try that.
This surely works but I think it should work with l(k) at least as well as with square brackets as round parenthesis are ususally used for these purposes.
Arno
Find all posts by this user
Quote this message in a reply
10-11-2017, 02:34 PM
Post: #7
RE: Problem with a list in CAS-program
I've asked this question before with (), {}, [] etc, I've only seen () used to index matrices or lists, so where is [] being used to index a list coming from?????
There needs to be consistency in the indexing of variables of a certain type. What are we supposed to do, guess as to which brackets to use?
...i.e. Where in the documentation does it refer to using [] to index a list?
Thx
Find all posts by this user
Quote this message in a reply
10-11-2017, 04:16 PM
Post: #8
RE: Problem with a list in CAS-program
I had read that thread concerning different brackets but totally forgot about it. I think somewhere in the manuals usage of () for indices is mentioned and it always is used in most programming languages I know, so thisis, at least in my eyes, a bug.
Quite funny, I revisited the user guide to look for more information and found only explanation for list usage in home-view, nothig about lists in CAS.
Arno
Find all posts by this user
Quote this message in a reply
10-11-2017, 06:45 PM
Post: #9
RE: Problem with a list in CAS-program
The square brackets [] are used in the xcas documentation for list indexing.

On the Prime the parentheses () work well in CAS with a numerical index :
Code:
l1:=MAKELIST(1,I,1,8)
l1(3):=l1(3)+1

   

However it doesn't work in CAS when the index is a variable as it is parsed as something different :
Code:
k:=3
l1(k):=l1(k)+1
is converted to:
Code:
l1:=(k)->l1(k)+1
after a warning message "l1: recursive definition"

   
Find all posts by this user
Quote this message in a reply
10-11-2017, 07:21 PM (This post was last modified: 10-11-2017 07:26 PM by Arno K.)
Post: #10
RE: Problem with a list in CAS-program
Quite clear, that collides with one possibility of defining a function in CAS, even after the CAS could see that l1 is a list, perhaps one can work at that point, but maybe ist difficult for the programmer to get that done, cas could get strict-types at declaration time which is unconvenient for the user, so the other possibilty is to check the construction for this recursive definition and if this error occurs CAS can simply use the assignment, which can be wrong, too.
But this will be Bernards task, I think.
Arno
Find all posts by this user
Quote this message in a reply
10-11-2017, 07:54 PM
Post: #11
RE: Problem with a list in CAS-program
The CAS parser is using [] for list/vector/array index like many programming languages. () is accepted for read access, but for write access, then definition of a function by f(x):=expression has priority.
Find all posts by this user
Quote this message in a reply
10-11-2017, 09:38 PM
Post: #12
RE: Problem with a list in CAS-program
So I will use [] instead of () in my CAS-programs and Home-programs, that is no problem for me, but as stated before I had in mind, before writing the program, that in the user guide only () were mentioned for access to list objects (ok, forgot that there in HOME is explicitely used AND CAS is omitted). I noted that in my private user guide, that will help in the future).
Not to you Bernard, but to the HP responsables:
Exactly that is what has to be done: add things like that to the user guide so those problems can be avoided before they arise, here I think of the target group and pupils/students trying some task like the one I wanted to solve and ending up with: nice looking but...
Arno
Find all posts by this user
Quote this message in a reply
10-12-2017, 01:18 AM (This post was last modified: 10-12-2017 01:21 AM by webmasterpdx.)
Post: #13
RE: Problem with a list in CAS-program
So, let me get this straight, for home list access we can use () always, even if the index is a variable.
However, in a cas program or in cas mode, we must use [] if the index is a variable....thus we probably should use [] in cas mode always, or in a cas program.
I don't have a problem with this, just lets be clear about this and stick with it....
Is my understanding correct?

One more question....is this also true in vector/matrix indexing?
Find all posts by this user
Quote this message in a reply
10-12-2017, 06:20 AM (This post was last modified: 10-12-2017 06:20 AM by parisse.)
Post: #14
RE: Problem with a list in CAS-program
There is (almost) no difference in CAS, vectors are lists, matrices are lists of lists of the same size.
Find all posts by this user
Quote this message in a reply
10-29-2017, 04:55 PM
Post: #15
Problem with a list in CAS-program
This may generate an error because you want to add a single item to a list, which will correctly result in an error. I think it's better to address the next element of the list with l (K + 1). I am missing the assignment of the value to the list l (k + 1).
The program works now. But the algorithm must still be adjusted. I did not check it.
Best regards, Rudi

Code:
#cas
POSDIVNF(n):=
BEGIN
LOCAL l,p,j,m,k0;
l:={}; //MAKELIST(1,X,1,n);
PRINT();
FOR j FROM 2 TO n DO
p:=1;
m:=j;
WHILE p≤j DO
p:=nextprime(p);
WHILE irem(m,p)==0 DO
m:=iquo(m,p);
l:=append(l,p+1);
END;
END;
END;
return ΠLIST(l);
END;
#end
Find all posts by this user
Quote this message in a reply
10-29-2017, 06:54 PM
Post: #16
RE: Problem with a list in CAS-program
As Rudi does not know what the by him mentioned program does I add this working code with german comments:
Es berechnet die Anzahl aller Teiler von n!
Code:
 #cas
POSDIVNF(n):=
BEGIN
LOCAL l,p,j,m;
p:=1;
l:= MAKELIST(1,X,1,n);//maximal kann n der größte Primfaktor von n! sein, Liste von 1en machen
 FOR j FROM 2 TO n DO  //bei 2 anfangen die Faktoren von n! zu untersuchen
  p:=1;
m:=j;
   WHILE p≤j DO  //höchstens bis zum aktuellen Faktor können Primzahlen auftreten
     p:=nextprime(p);
      WHILE irem(m,p)==0 DO //solange p m teilt
      m:=iquo(m,p); //wird dividiert
       l[p]:=l[p]+1; //und 1 zur aktuellen Position in der Liste addiert
      END;
   END;
 END;
  return ΠLIST(l);// die Gesamtzahl positiver Faktoren von n! zurückliefern
END;
#end
Arno
Find all posts by this user
Quote this message in a reply
10-30-2017, 06:11 AM
Post: #17
RE: Problem with a list in CAS-program
Hello,

CAS has/had extra syntax for historical reasons (the [] based one).
CAs historically did not really have lists, only vectors []. Lists as curly bracket items where added for Prime (well, the 39GII+ to be precise)...

However, the advise on prime is to only use {} for lists and () for list variable access as they will be cross compatible between the CAS and home.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
10-30-2017, 06:29 AM
Post: #18
RE: Problem with a list in CAS-program
Inside the CAS, I recommend to use [] for vector/list/whatever access, because () in write access could default to function definition.
Find all posts by this user
Quote this message in a reply
10-30-2017, 09:08 AM
Post: #19
RE: Problem with a list in CAS-program
The HP Prime wiki has a CAS vs HOME article linked to from the Programming Documentation section and in there it says:

Indexing in HP Basic is done using () braces, as in L1(5). In CAS, () can be used with a constant index in read only mode, but when a variable index is used, due to parsing issues (it thinks L1(x) is a function call), you must use [] braces instead, as in L1[i]. So, it is recommended if in CAS mode or in a CAS program, it is best to use [] to index always. This applies to Lists, Matrices and Vectors (1D Matrix). In XCas, lists, matrices and vectors are all lists. Note that XCas uses [] always anyway, so this keeps the Prime's CAS compatible with XCas.

http://www.wiki4hp.com/doku.php?id=prime:start
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)