Menu

#3995 read_list fails on objects with operands

None
not-a-bug
nobody
5
2022-06-24
2022-06-23
Jimis Hol
No

read_list or read_nested_list functions do not work as expected, when object includes operands.
I may use these functions wrongly but for now my workaround is to store strings and evaluate those strings later.
In next simple example i expected x to be equal to y but 5th element 1/3 is not loaded correctly . By my workaround z is actually equal to x.
Here is the simple example:

(%i3)   x:makelist(random(9),i,5)$ x[5]:1/3$ display(x)$
x=[2,6,2,5,1/3]
(%i4)   write_data(x, "~/test_read_list.txt", ",")$
(%i5)   y:read_list("~/test_read_list.txt", ",")$ 
(%i6)   display(y)$
y=[2,6,2,5,1,/,3]
(%i9)   w:copylist(x)$ w[5]:"1/3"$ display(w)$
w=[2,6,2,5,"1/3"]
(%i10)  write_data(w, "~/test_read_list.txt", ",")$
(%i11)  z:read_list("~/test_read_list.txt", ",")$
(%i12)  for k: 1 thru length(z) do if stringp(z[k]) then z[k]:eval_string(z[k])$
(%i13)  display(z)$
z=[2,6,2,5,1/3]

Discussion

  • Robert Dodier

    Robert Dodier - 2022-06-23

    Hi Jimis, thanks for your interest in Maxima. Do I understand correctly that you are creating the data file which is later to be read back into Maxima? i.e., you are not reading a file that was created by someone else using some system other than Maxima.

     
    • Jimis Hol

      Jimis Hol - 2022-06-23

      Thank you for the quick reply.
      You understand well. I make a list with maxima, i store and then read
      back. As in the example. Since i worked around it, it is not a big
      problem but i wanted to inform developers that i expected a 'correct'
      for me behaviour.
      Thank you

       

      Last edit: Robert Dodier 2022-06-24
  • Jimis Hol

    Jimis Hol - 2022-06-23

    Thank you for the quick reply.
    You understand well. I make a list with maxima, i store it and then read it
    back. As in the example. Since i worked around it, it is not a big
    problem for me but i wanted to inform developers that i expected a 'correct'
    for me behavior that would be x[5]=y[5] in the above case.
    Thank you

     
  • Stavros Macrakis

    The documentation of read_list is very poor, so it's hard to know what it's supposed to be doing.

    The intent seems to be to read the input file as a series of Maxima tokens . This is stated nowhere in the documentation. It parses tokens greedily, and does not require a separator between tokens. Numerical tokens are interpreted as numbers, and all others are interpreted as strings. Note that Maxima syntax considers the rational number 1/2 to be composed of three tokens, not one.

    For example:

    abc => ["abc"]
    abc123 => ["abc123"]
    123abc => [123, "abc"]
    2..3 => [2, 0.3]
    2...3 => [2, ".", 0.3]
    2e3 => [2000.0]
    1/2 => [1, "/", "2"]  <<< these are three Maxima tokens
    1//2 => [1, "/", "/", 2]
    1^^2 => [1, "^^", 2]  << ^^ is a single Maxima token
    

    It gives errors for the following cases, because it expects an exponent:

    2e => error
    2e* => error
    

    It doesn't clean up state correctly after errors:

    2e* => error
    x => ["*", "x"]
    

    Apparently the lookahead character stays in the input buffer. The other behavior we can consider strange; this one is clearly a bug.

    This behavior seems very idiosyncratic, and I have no idea why anyone would find it useful, but that's what it does.

    So I guess this is "not a bug", though the documentation clearly needs to be improved.

     

    Last edit: Stavros Macrakis 2022-06-23
  • Stavros Macrakis

    • status: open --> not-a-bug
     
  • Gunter Königsmann

    I believe read_list currently only reads floating -point numbers. If you want more flexibility you can use openr read_line, schoncat and Close in order to write a function that reads in comma-separated maxima commands.

     
  • Stavros Macrakis

    Its behavior is bizarre and its name is misleading, but read_list reads tokens other than floating-point numbers -- see my posting above.

     
  • Robert Dodier

    Robert Dodier - 2022-06-24

    Jimis, there are various ways to achieve the behavior you want -- saving values and restoring them. I apologize there isn't a single best way, but I hope at least one of these works for you. I'll recommend save for simplicity.

    save writes values into a file which can then be read by load. Values are associated with the name of the variable, so when you execute load, the values are assigned to the corresponding variables. The file is a Lisp file, so it can be edited, although the format is obscure.

    I have some other ideas which I'll address later today.

     
  • Jimis Hol

    Jimis Hol - 2022-06-24

    Well maxima was a valuable companion for years on my hobbies. Mathematics and a touch of programming. It helped me with ctensors, differential geometry, algebraic systems and many others that i now forget. However, because i use it casually, depending on my interest each time, i forget how to use it and remember only what maxima can do for me. Same happens to by interests too. This time i decided to read about game theory. I had the feeling that maxima could help me to solve the exercises from the book i read. Without expecting to succeed, I finally finished what I wanted to accomplish. So, mr Robert, I highly doubt I will need this feature again.

    Taking advantage of the 'Discussion' label over these posts and the interest, all you show, in this and other tickets, i would like to ask not about input or output on maxima, because i think i finished my effort, but how to advertise my work to other users of maxima that may be interest to do the same thing i did, but with much more efficient code. My work is at https://github.com/jimishol/Nash-equilibria-by-maxima-CAS.git
    It seems to work for small games as long as memory is sufficient. I am proud of it, because I overextended myself to achieve it, but at the same time i am ashamed of the stupid 'for' loops that was constructed through evaluation of strings, specially in the pure_Nash.wxm file.

    Thank you
    not only for the replies but also because you keep maxima alive for me and other users that like it and use it.

     

Log in to post a comment.