Recasting Vectors

9 views
Skip to first unread message

Sid Andal

unread,
Apr 18, 2023, 10:48:48 AM4/18/23
to FriCAS - computer algebra system

The following two vectors

U := [2, 3, 4]
V := [7, 8, 9]

are in a 3-Dim Alg over the PAdicRational(13).

Is there a way to rewrite their product

        2                          2                    3
W := [6 + 13 + 7 13 , 1 + 9 13 + 7 13 , 8 13 + 7 13 ]

in a different form where the powers of 13 are factored out of the brackets
per the following?

                                                                  2         3
W  := [6, 1, 0] + [1, 9, 8] 13  + [7, 7, 0] 13 + [0, 0, 7] 13

Thanks,
SWA

Ralf Hemmecke

unread,
Apr 18, 2023, 12:50:47 PM4/18/23
to fricas...@googlegroups.com
When I asked for code, I actually meant some fricas input by which I can
at least get your first W representation.

I do not, however know, how you computed your W. I get the following.

PAR ==> PAdicRational(13);
DP ==> DirectProduct(3, PAR);
u: DP := directProduct [2, 3, 4];
v: DP := directProduct [7, 8, 9];
(36) -> w := u*v

(36) [1 + 13, 11 + 13, 10 + 2 13]
Type: DirectProduct(3,PAdicRational(13))

Anyway, the problem with printing this in the form you want, is that
PAdicRational(13) simply has not enough functionality.

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

If you only worked with a DirectProduct of PAdicInteger, then you can
find a digits: % -> Stream Integer function from which one could program
the respective output function.

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

Unfortunately PAdicRational does not export something to get hold of the
exponent or the stream data.

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

Ralf

Sid Andal

unread,
Apr 19, 2023, 2:55:55 PM4/19/23
to FriCAS - computer algebra system
Since, digits works with PAdicInteger only, one way is to try converting from PAdicRational to PAdicInteger, then apply digits:

(3) -> PR ==> PAdicRational(11)
(4) -> PI   ==> PAdicInteger(11)
(5) ->
(5) -> x : PR := 1234

                                      2
   (5)  2 + 2 11 + 10 11
(6) ->
(6) -> y : PI := approximate(x, 3) :: PI

                                      2
   (6)  2 + 2 11 + 10 11
(7) ->
(7) -> digits(y)

   (7)  [2, 2, 10]
(8) ->

Is there a more direct or more efficient way to convert a PAdicRational to PAdicInteger?

SWA

Ralf Hemmecke

unread,
Apr 19, 2023, 3:48:40 PM4/19/23
to fricas...@googlegroups.com
> Is there a more direct or more efficient way to convert a PAdicRational to
> PAdicInteger?

Officially, no.

However, there are two ways to get all digits of your rational number.

A) Provide a patch that exposes the representation of PAdicRactional(p)
and fight for it to be included into FriCAS.

B) Get the source code from src/algebra/padic.spad and move some lines
(see below). (Which would give you (A) if you properly put this in your
clone of your github repo of fricas and start a pull request.) Maybe you
should come up with some more telling names for the exported functions.


Look at
https://github.com/fricas/fricas/blob/master/src/algebra/padic.spad#L522
and
https://github.com/fricas/fricas/blob/master/src/algebra/padic.spad#L339
you learn that the representation is given by
https://github.com/fricas/fricas/blob/master/src/algebra/padic.spad#L377

Rep := Record(expon : I, pint : PADIC)

So all you have to do is to move the lines

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

just before

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

Then type

)compile padic.spad

and suddenly

PAR ==> PAdicRational 7

(13) -> r := (- 3/49)::PAR

(13)
- 2 - 1 2 3 4 5 6
7 8
4 7 + 6 7 + 6 + 6 7 + 6 7 + 6 7 + 6 7 + 6 7 + 6 7 + 6 7
+ 6 7
+
9
O(7 )
Type: PAdicRational(7)
(14) -> getExpon r

(14) - 2
Type: Integer
(15) -> digits getZp r

(15) [4, 6, 6, 6, 6, 6, 6, 6, 6, 6, ...]
Type: Stream(Integer)


Now for achieving your initial goal in 3 dimensions, you would have to
do a little programming. I put this as an exercise. ;-)

Ralf

Waldek Hebisch

unread,
Apr 19, 2023, 7:45:40 PM4/19/23
to fricas...@googlegroups.com
On Wed, Apr 19, 2023 at 11:55:55AM -0700, Sid Andal wrote:
> Since, digits works with PAdicInteger only, one way is to try converting
> from PAdicRational to PAdicInteger, then apply digits:
>
> (3) -> PR ==> PAdicRational(11)
> (4) -> PI ==> PAdicInteger(11)
> (5) ->
> (5) -> x : PR := 1234
>
> 2
> (5) 2 + 2 11 + 10 11
> (6) ->
> (6) -> y : PI := approximate(x, 3) :: PI
>
> 2
> (6) 2 + 2 11 + 10 11
> (7) ->
> (7) -> digits(y)
>
> (7) [2, 2, 10]
> (8) ->
>
> Is there a more direct or more efficient way to convert a PAdicRational to
> PAdicInteger?

If you _know_ that your p-adic rational in fact is an p-adic integer,
then official idiom is

retract(x)@PI

Unfortunately, ATM retract for p-adic rationals is unimplemented. But
it would be easy to add it.

In general,

x - approximate(x, 0)::PR

is a p-adic integer, so you can split p-adic rational into integer
part and fractional part.

--
Waldek Hebisch

Waldek Hebisch

unread,
Apr 19, 2023, 8:06:30 PM4/19/23
to fricas...@googlegroups.com
You did not write what your really want. Do you want internal
structure to be as above? If yes, than you need new domain
for vectors (or whatever aggregate you use). Such domain would
have to do similar things to what PAdicRational is doing, but
using stream of vectors (or more general aggregates).

If all what you want is printing, then you should be able to
produce printouts using digits extraction (say via 'approximate').

--
Waldek Hebisch

Sid Andal

unread,
Apr 22, 2023, 5:41:55 PM4/22/23
to FriCAS - computer algebra system
The following 2D example describes the problem:

PR ==> PAdicRational(11)
SQ ==> SQMATRIX(2, PR)
LS ==> List SQ

M : SQ := [[10, 7], [3, 8]]

        ┌10  7┐
   (4)  │     │
        └3   8┘

N : SQ := [[9, 2], [5, 1]]

        ┌9  2┐
   (5)  │    │
        └5  1┘

S : LS := [M, N]

         ┌10  7┐  ┌9  2┐
   (6)  [│     │, │    │]
         └3   8┘  └5  1┘

AP := ALGSC(PR, 2, ['A, 'B], S)

   (7)
  AlgebraGivenByStructuralConstants(PAdicRational(11),2,[A,B],[[[10,7],[3,8]],[
  [9,2],[5,1]]])

V := basis()$AP

   (8)  [A, B]
(A, B) := (V.1, V.2)

   (9)  B

)clear p M N S V
 
(10) -> (U, V, W) : AP
(11) -> U := 9*A + 10*B

   (11)  10 B + 9 A
(12) -> V := 8*A + 7*B

   (12)  7 B + 8 A
(13) -> W := U*V

                                         2                                   2       3
   (13)  (1 + 3 11 + 10 11 )B + (3 + 2 11 + 5 11  + 11 )A
(14) ->

All scalar coefs are padic integers and an algebra over the ring
of padic integers would've been sufficient. However, ALGSC requires
the Ring to be a Field, so, PAdicRational was chosen, instead.

The problem now is to rewrite the product W into the following form
or into something equivalent:

                                  2        3
(B + 3A) + (3B + 2A)11 + (10B + 5A)11 + A 11

where the powers of the prime (11, in this case) are factored out.

SWA

Waldek Hebisch

unread,
Apr 26, 2023, 10:19:37 PM4/26/23
to fricas...@googlegroups.com
Well, basic things in ALGSC work for CommutativeRing, it is relatively
simple change to allow CommutativeRing. Field is needed to have
equation solvers, which is needed for example for LeftUnit. In
principle equation solving can be done for PrincipalIdealDomain,
so for Integer or PAdicInteger. However, that would require more
work.

> The problem now is to rewrite the product W into the following form
> or into something equivalent:
>
> 2 3
> (B + 3A) + (3B + 2A)11 + (10B + 5A)11 + A 11
>
> where the powers of the prime (11, in this case) are factored out.

You did not write what you consider "rewrite"? Basically, in FriCAS
you need a domain to represent your values. I attach a simple domain
which is only good for printing, but you probably want more.

To try it you need newest FriCAS from git ('retract' from padic
rationals to padic integer is implemented only in git version).
Compile the attached Spad file. Then ')read' the attached
input file. Final line of outut is:

1 2 11
(17) B + 3 A + (3 B + 2 A)11 + (10 B + 5 A)11 + O(11 )
Type: ModuleTaylorSeries(Fraction(Integer),AlgebraGivenByStructuralConstants(Fraction(Integer),2,[A,B],[[[10,7],[3,8]],[[9,2],[5,1]]]),theMap(*1;my_pr;1;frame1)

which IIUC is what you want

--
Waldek Hebisch
pads.spad
als.input

Sid Andal

unread,
May 3, 2023, 12:14:48 PM5/3/23
to FriCAS - computer algebra system
Downloaded the padic.spad from github and made the recommended modifications (moving up lines with getExpon and getZp) and compiled.

In addition, the attached file, als.input, was also modified per the following:


pR := PAdicRational(11)
pI := PAdicInteger(11)
ll := [[[10, 7], [3, 8]], [[9, 2], [5, 1]]]
AP := ALGSC(pR, 2, ['A, 'B], ll)
qF := Fraction(Integer)
BP := ALGSC(qF, 2, ['A, 'B], ll)
V := basis()$AP
(A, B) := (V(1), V(2))

gen_digs2(d1 : Stream(Integer), d2 : Stream(Integer)) : Stream(BP) ==
  (delay$Stream(BP))(
    () : Stream(BP) +->
         ed1 := empty?(d1)
         ed2 := empty?(d2)
         ed1 and ed2 => empty()$Stream(BP)
         c1 : Integer
         if ed2 then
             c1 := 0
         else
             c1 := frst(d1)
             d1 := rst(d1)
         c2 : Integer
         if ed2 then
             c2 := 0
         else
             c2 := frst(d2)
             d2 := rst(d2)
         bv := represents(vector([c1::qF, c2::qF]))$BP
         cons(bv, gen_digs2(d1, d2))
   )

my_pr(n : Integer) : OutputForm == 11::OutputForm ^ (n::OutputForm)

mS := ModuleTaylorSeries(qF, BP, my_pr)

dW(x) == digits(retract(x)@pI)

pds(W : AP) : mS ==
    cW := coordinates(W)
    series(gen_digs2(dW(cW(1)), dW(cW(2))))$mS


Here are a few examples of the applications of pds on vectors:


(14) -> D : AP := 654 * A + 488 * B

                            2                                  2
   (14)  (4 + 4 11 )B + (5 + 4 11 + 5 11 )A
(15) -> typeOf(D)

   (15)

  AlgebraGivenByStructuralConstants(PAdicRational(11),2,[A,B],[[[10,7],[3,8]],[
  [9,2],[5,1]]])
(16) ->
(16) -> pds(D)
   Compiling function dW with type PAdicRational(11) -> Stream(Integer)
     
   Compiling function gen_digs2 with type (Stream(Integer), Stream(
      Integer)) -> Stream(AlgebraGivenByStructuralConstants(Fraction(
      Integer),2,[A,B],[[[10,7],[3,8]],[[9,2],[5,1]]]))
   Compiling function pds with type AlgebraGivenByStructuralConstants(
      PAdicRational(11),2,[A,B],[[[10,7],[3,8]],[[9,2],[5,1]]]) ->
      ModuleTaylorSeries(Fraction(Integer),
      AlgebraGivenByStructuralConstants(Fraction(Integer),2,[A,B],[[[10
      ,7],[3,8]],[[9,2],[5,1]]]),theMap(*1;my_pr;1;frame1))

                                           1                          2
   (16)  4 B + 5 A + 4 A 11  + (4 B + 5 A)11
(17) ->


However, in the following example it complains:


(17) -> F : AP := 1234 * A + 5678 * B

                                         2           3                                     2
   (17)  (2 + 10 11 + 2 11  + 4 11 )B + (2 + 2 11 + 10 11 )A
(18) ->
(18) -> pds(F)

 
   >> System error:
   Argument X is not a NUMBER: "NullStream"

(18) ->

It seems pds works on some but not on all vectors.

Waldek Hebisch

unread,
May 3, 2023, 3:12:55 PM5/3/23
to fricas...@googlegroups.com
On Wed, May 03, 2023 at 09:14:48AM -0700, Sid Andal wrote:
> Downloaded the padic.spad from github and made the recommended
> modifications (moving up lines with getExpon and getZp) and compiled.
>
> In addition, the attached file, als.input, was also modified per the
> following:
>
> gen_digs2(d1 : Stream(Integer), d2 : Stream(Integer)) : Stream(BP) ==
> (delay$Stream(BP))(
> () : Stream(BP) +->
> ed1 := empty?(d1)
> ed2 := empty?(d2)
> ed1 and ed2 => empty()$Stream(BP)
> c1 : Integer
> if ed2 then
^^^
Sorry, this was silly typo, should be ed1
<snip>
> However, in the following example it complains:
>
>
> (17) -> F : AP := 1234 * A + 5678 * B
>
> 2 3
> 2
> (17) (2 + 10 11 + 2 11 + 4 11 )B + (2 + 2 11 + 10 11 )A
> (18) ->
> (18) -> pds(F)
>
>
> >> System error:
> Argument X is not a NUMBER: "NullStream"

This is due to typo above.

FYI: 'frst' on a stream should be called only if 'empty?'
returns false. If 'empty?' returns true, then there are no
more terms, so we use 0 instead.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages