Problem with the minus sign in symbolic expression

24 views
Skip to first unread message

Anastasia Theodouli

unread,
Dec 8, 2016, 4:49:04 AM12/8/16
to sage-support
Hi all!

In my code, I have a multivariate polynomial with a sign symbol which doesn't return the correct result. I.e.

The code snippet is as follows:

                             
    sage: f1=b1-a1.dot_product(x)

a1 is a vector with values (76, 83), b1 is scalar with value 62, x is a vector with values (x1,x2). Thus, f1 should return **62-76 * x1-83 * x2**; however it returns **25*x1 + 18*x2 + 62** which is not correct.

Please note that if I use **addition** in f1 as follows:
  
                                
    sage: f1=b1+a1.dot_product(x)
The result is correct,i.e. 76*x1 + 83*x2 + 62

 type(f1) is sage.symbolic.expression.Expression

What could the problem be? I tried to convert the symbolic expression to multivariate polynomial, but this didn't work either? I also though there might be a problem with the minus symbol in my PC but couldn't solve it  this way either.

Thank you for your responses!
Regards,
Natassa

John Cremona

unread,
Dec 8, 2016, 4:56:04 AM12/8/16
to SAGE support
You should say which Sage version you are using, and also give the
complete code. In Sage 7.4 I get this:

sage: var('x1','x2')
(x1, x2)
sage: a1=vector([76,83])
sage: b1=62
sage: x=vector([x1,x2])
sage: f1=b1-a1.dot_product(x)
sage: f1
-76*x1 - 83*x2 + 62

which looks good. Perhaps you mistyped something?

John Cremona

On 8 December 2016 at 09:49, Anastasia Theodouli
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.

Anastasia Theodouli

unread,
Dec 8, 2016, 5:08:21 AM12/8/16
to sage-support
My Sage version is 6.9

The exact code I use is as follows:

  A1 = [(', '.join('x%i'%i for i in [1.. n]))]; ### construct a suitable multivariate ring
    V
= var(A1[0])                               ### define a str variable
    x
=vector(list(V))                                 ### convert to vector
   
f1=b1-a1.dot_product(x)  

fi returns
25*x1 + 18*x2 + 62
instead of

76*x1 + 83*x2 + 62

Thank you!
Regards,
Natassa

Anastasia Theodouli

unread,
Dec 8, 2016, 5:34:25 AM12/8/16
to sage-support

Anastasia Theodouli

unread,
Dec 8, 2016, 5:35:40 AM12/8/16
to sage-support
UPDATE: The problem was that I was working modulo 101 when I was defining a1 and b1.

On Thursday, December 8, 2016 at 11:49:04 AM UTC+2, Anastasia Theodouli wrote:

John Cremona

unread,
Dec 8, 2016, 7:06:23 AM12/8/16
to SAGE support
On 8 December 2016 at 10:35, Anastasia Theodouli
<anastasia...@gmail.com> wrote:
> UPDATE: The problem was that I was working modulo 101 when I was defining a1
> and b1.

I think that trying to combine symbolic variables (x1 and x2) with
modular arithmetic is known to create difficulties. If you want to do
the computation mod 101 then something like

F=GF(101)
R.<x1,x2> = F[]

# The above 2 lines tell Sage that x1, x2 are independent variables
over F (R is a polynomial ring in these 1 variables)

a1=vector([76,83])
b1=62
x=vector([x1,x2])
f1=b1-a1.dot_product(x)
f1

gives

25*x1 + 18*x2 - 39

(I am not sure why 62 displays as -39 here)

John

slelievre

unread,
Dec 8, 2016, 8:22:08 AM12/8/16
to sage-support
Thu 2016-12-08 13:06:23 UTC+1, John Cremona:

> I think that trying to combine symbolic variables (x1 and x2) with
> modular arithmetic is known to create difficulties.  If you want to do
> the computation mod 101 then something like
>
> F = GF(101)
> R.<x1,x2> = F[]
> # The above 2 lines tell Sage that x1, x2 are independent variables
> over F (R is a polynomial ring in these 1 variables)
>
> a1 = vector([76, 83])
> b1 = 62
> x = vector([x1, x2])
> f1 = b1 - a1.dot_product(x)
> f1
>
> gives
>
> 25*x1 + 18*x2 - 39
>
> (I am not sure why 62 displays as -39 here)

It seems it is the standard display for coefficients
in polynomial rings over a finite field.

See for instance (with R as in your code):

    sage: [R(i) for i in range(0, 101, 10)]
    [0, 10, 20, 30, 40, 50, -41, -31, -21, -11, -1]

Not for fields elements, though.

    sage: [F(i) for i in range(0, 101, 10)]
    [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

slelievre

unread,
Dec 8, 2016, 8:28:11 AM12/8/16
to sage-support
Reply all
Reply to author
Forward
0 new messages