Problem reading output file

11 views
Skip to first unread message

Sid Andal

unread,
Apr 15, 2023, 11:18:13 AM4/15/23
to FriCAS - computer algebra system
A matrix of type PAdicRational is defined and saved in a file. However,
attempting to read it back leads to an error:

PR ==> PAdicRational(13)
SQ ==> SQMATRIX(4, PR)

A : SQ := [[2, 3, 5, 0], [7, 11, 12, 4], [3, 8, 6, 10], [12, 9, 8, 5]]

typeOf A

SquareMatrix(4,PAdicRational(13))

f : File SQ := open("PaMat4x4.out", "output")

write!(f,A)
close!(f)


$ cat PaMat4x4.out

#2A(((0 2 #1="NullStream") (0 3 #1#) (0 5 #1#) (0 #1#))
    ((0 7 #1#) (0 11 #1#) (0 12 #1#) (0 4 #1#))
    ((0 3 #1#) (0 8 #1#) (0 6 #1#) (0 10 #1#))
    ((0 12 #1#) (0 9 #1#) (0 8 #1#) (0 5 #1#)))


$ cp PaMat4x4.out PaMat4x4.input


fn := filename("", "PaMat4x4", "input")$FileName

f := open(fn, "input")$File(SQ)

B : SQ := read!(f)

>> System error:
   The value "NullStream" is not of type INTEGER.

Removing the string "NullStream" doesn't seem to help....it leads to
other types of errors.

Thanks,
SWA

Ralf Hemmecke

unread,
Apr 15, 2023, 11:41:18 AM4/15/23
to fricas...@googlegroups.com
Look at

https://fricas.github.io/api/PAdicRational.html

PAdicRational is build on InnerPAdicInteger. And that in turn is
implemented as Stream(Integer).

https://github.com/fricas/fricas/blob/master/src/algebra/padic.spad#L77

So, in generaal, FriCAS has to save and read a stream. While this is not
completely impossible (because a stream is also in memory just described
with a finite amount of data), the way it is done via your write!(f,A)
cannot work, since that does obviously only write out the first few
values of the stream. In fact, "NullStream" is a special way to denote
"the end of the stream". Unfortunately, FriCAS doesn't seem to handle
this case well when reading your file.

Since some streams are finite, the situation could be cured for them,
but what would it help? Potentially, streams are infinity and to do it
properly, the file should contain exactly the data that produces the
stream, i.e., the first few values together with a PROGRAM that computes
any following value.

Does that explain the problem?

Ralf

Sid Andal

unread,
Apr 15, 2023, 12:32:16 PM4/15/23
to FriCAS - computer algebra system
Yes, thanks.

As a workaround, it seems possible to save the matrix as an integer matrix and then read it back and assign it to a matrix of type PAdicRational.

SWA

Waldek Hebisch

unread,
Apr 15, 2023, 12:34:24 PM4/15/23
to fricas...@googlegroups.com
On Sat, Apr 15, 2023 at 05:41:15PM +0200, Ralf Hemmecke wrote:
> Look at
>
> https://fricas.github.io/api/PAdicRational.html
>
> PAdicRational is build on InnerPAdicInteger. And that in turn is implemented
> as Stream(Integer).
>
> https://github.com/fricas/fricas/blob/master/src/algebra/padic.spad#L77
>
> So, in generaal, FriCAS has to save and read a stream. While this is not
> completely impossible (because a stream is also in memory just described
> with a finite amount of data), the way it is done via your write!(f,A)
> cannot work, since that does obviously only write out the first few values
> of the stream.

Actually, there is special code which is supposed to handle writing
streams to files. ATM I can not say if this is a regression or maybe
nobody spend enough time to make this really work, but what Sid Andal
sees is a bug, that is thing which is supposed to work, but does not
work.

--
Waldek Hebisch

Waldek Hebisch

unread,
Apr 15, 2023, 1:06:00 PM4/15/23
to fricas...@googlegroups.com
As Ralf noted, padic numbers need special support to read/write.
For some reason (maybe bug) this support is not active when using
normal files. However, it is active in Library. You can use it like:

(1) -> ll := library("tst.lib")

(1) "tst.lib"
Type: Library
(2) -> PR ==> PAdicRational(13)
Type: Void
(3) -> A : PR := 1

(3) 1
Type: PAdicRational(13)
(4) -> ll("a") := A

(4) 1
Type: PAdicRational(13)
(5) -> Al : List(PR) := [1, 1/4]

(5)
[1,

2 3 4 5 6 7 8 9
10 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13
+
10 11
9 13 + O(13 )
]
Type: List(PAdicRational(13))
(6) -> ll("Al") := Al

(6)
[1,

2 3 4 5 6 7 8 9
10 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13
+
10 11
9 13 + O(13 )
]
Type: List(PAdicRational(13))
(7) -> Am : SQMATRIX(4, PR) := [[2, 3, 5, 0], [7, 11, 12, 4], [3, 8, 6, 10], [12Am : SQMATRIX(4, PR) := [[2, 3, 5, 0], [7, 11, 12, 4], [3, 8, 6, 10], [12, 9, 8, 5]]

+2 3 5 0 +
| |
|7 11 12 4 |
(7) | |
|3 8 6 10|
| |
+12 9 8 5 +
Type: SquareMatrix(4,PAdicRational(13))
(8) -> ll("Am") := Am

+2 3 5 0 +
| |
|7 11 12 4 |
(8) | |
|3 8 6 10|
| |
+12 9 8 5 +
Type: SquareMatrix(4,PAdicRational(13))
(9) -> keys(ll)

(9) ["Am", "Al", "a"]
Type: List(String)
(10) -> close!(ll)

(10) "tst.lib"
Type: Library

In new session:

(1) -> ll := library("tst.lib")

(1) "tst.lib"
Type: Library
(2) -> keys(ll)

(2) ["Am", "Al", "a"]
Type: List(String)
(3) -> ll("a")

(3) 1
Type: PAdicRational(13)
(4) -> ll("Al")

(4)
[1,

2 3 4 5 6 7 8 9
10 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13 + 9 13
+
10 11
9 13 + O(13 )
]
Type: List(PAdicRational(13))
(5) -> ll("Am")

+2 3 5 0 +
| |
|7 11 12 4 |
(5) | |
|3 8 6 10|
| |
+12 9 8 5 +
Type: SquareMatrix(4,PAdicRational(13))


--
Waldek Hebisch

Ralf Hemmecke

unread,
Apr 15, 2023, 1:26:07 PM4/15/23
to fricas...@googlegroups.com
> As Ralf noted, padic numbers need special support to read/write.
> For some reason (maybe bug) this support is not active when using
> normal files.

Well, should it be active? Now that you showed what I discussed in my
answer mail (thanks a lot for this demonstration -- I should add it to
the help section of FriCAS), I guess we should make it work, since it is
a really cool feature to be able to save and load potentially infinity
objects.

Actually, now I understand why File saves lisp expression and not the
OutputForm.

Ralf
Reply all
Reply to author
Forward
0 new messages