Point addition and multiplication on Sage - sage

I am trying to compute rP+r'Q on Sage where r,r' are positive integers and
P=(38*a + 31 : 69*a + 77 : 1),
Q=(106*a + 3 : a + 103 : 1)
two points on the elliptic curve E:y^2=x^3-x over GF(107^2).
Now I tried to define P and Q on sage simply as I did here but this gives a syntax error. So how do I define points on Sage?

So the answer was rediculously simple. Although sage gives you the points (a: b: c), you have to define your point like (a, b, c). How did I just spend over 1h to find out.

Related

Choosing starting point in GNU Scientific Library multiroot finder

I'm using an implementation of the GNU Scientific Library's multiroot finder to solve for the unknowns (x and y) in the following system of non-linear equations:
I'm a bit confused, however, about the "starting point":
Solve(const double *x, int maxIter = 0, double absTol = 0, double relTol = 0) Find the root starting from the point X; Use the
number of iteration and tolerance if given otherwise use default
parameter values which can be defined by the static method SetDefault
How is the starting point chosen?
In one dimensional polynomial problem, this is a well-studied domain of Real-root_isolation. In two dimensions its less well know.
First note we can transform the equations into polynomials, take the first one
sqrt[(x-x1)^2 + (y-y1)^2] + s (t2 -t1) = sqrt[(x-x1)^2 + (y-y1)^2]
square both sides
[(x-x1)^2 + (y-y1)^2] + 2 sqrt[(x-x1)^2 + (y-y1)^2][s (t2 -t1)] + [s (t2 -t1)]^2 = [(x-x1)^2 + (y-y1)^2]
rearrange
2 sqrt[(x-x1)^2 + (y-y1)^2][s (t2 -t1)] = [(x-x1)^2 + (y-y1)^2] - [(x-x1)^2 + (y-y1)^2] - [s (t2 -t1)]^2
square again
4 [(x-x1)^2 + (y-y1)^2] [s (t2 -t1)]^2 = ([(x-x1)^2 + (y-y1)^2] - [(x-x1)^2 + (y-y1)^2] - [s (t2 -t1)]^2)^2
giving us a polynomial.(note)
Once we have a set of polynomial there are some algebraic techniques you can use.
A technique I use is the convert the polynomials into Bernstein polynomials. First, rescale your domain so both variables lie in [0,1]. If m, n are the degrees of the two polynomials then a polynomial can be expressed as the sum
sum i=0..m sum j=0..n b_ij mCi nCj x^i (1-x)^(n-i) y^j (1-y)^(n-j)
Where the mCi, nCj are binomial coefficients.
These polynomials have a nice convexity property. If the coefficients b_ij are all positive then the value of the polynomial will be positive for all x,y in [0,1]. Similar if the coefficients are all negative.
This allows you to say that "there is no solution in this region".
So a strategy to solve the problem might be:
pick the largest region the solutions might occur in.
Subdivide this into a set of smaller regions
For each region calculate the Bernstein polynomials for each of your equations
Examine the coefficients of the Bernstein polynomials, if they are all positive (or all negative) reject that region
You should now have rejected a large part of the domain. Start the multiroot finder using a point in each remaining region.
If you want details of how to construct Berstein polynomials you can read my paper at A new method for drawing Algebraic Surfaces
Note: by squaring we actually get more solutions that we want. In the initial problem we want the principle sqareroot, i.e. +sqrt(A), there are also solutions using the other root -sqrt(A). This makes the problem a bit easier rather than four solutions which we would get from the intersection of two hyperbolae, we just have the intersections of one branch so one or two solutions to the problem.
For your problem, there is quite a nice simple way to get one of the starting points.
Assume s=0. Then each equation will give the set of points that are equidistant from two points. This is a line, the perpendicular bisector of the line segment joining the points, then simply find the point of intersection of the three perpendicular bisectors. This will be the centre of the Circumscribed_circle. This might be enough for the algorithm. Even simpler is to simply take the average of the three points.

How to find the intecept x and y coordinates from 4 data points in Excel?

I have two points which form one line: (1,4) and (3,6), and another two which form another line: (2,1) and (4,2). These lines are continuous and I can find their intersection points by finding the equation for each line, and then equating them to find the x value at the intersection point, and then the y value.
i.e. for the first line, the equation is y = x + 3, and the second is y = 0.5x. At the intersection the y values are the same so x + 3 = 0.5x. So x = -6. Subbing this back into either of the equations gives a y value of -3.
From those steps, I now know that the intersection point is (-6,-3). The problem is I need to do the same steps in Excel, preferably as one formula. Can anyone give me some advice on how I would start this?
Its long but here it is:
Define x1,y1 and x2,y2 for the 1st line and x3,y3 and x4,y4 for the second.
x = (x2y1-x1y2)(x4-x3)-(x4y3-x3y4)(x2-x1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
y = (x2y1-x1y2)(y4-y3)-(x4y3-x3y4)(y2-y1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
Note that the denominators are the same. They will be ZERO! when the system has no solution. So you may want to check that in another cell and conditionally compute the answer.
Essentially, this formula is derived by solving a system of equations for x and y by hand using generic points (x1,y1), (x2,y2), (x3,y3), and (x4,y4). Easier yet, is solving the system by hand using well developed linear algebra concepts.
Wikipedia outlines this procedure well: Line-line intersection.
Also, this website describes all the different formulas and lets you put in whatever data you have in any mixed format and provides many details of the solutions: Everything about 2 lines.
Here's a matrix based solution:
x - y = -3
0.5*x - y = 0
Written as a matrix equation (I apologize for the poor typesetting):
| 1.0 -1.0 |{ x } { -3 }
| 0.5 -1.0 |{ y } = { 0 }
You can invert this matrix or use LU decomposition to solve it to get the answer. That method will work for any number of cases where you have one equation for each unknown.
This is easy to do by hand:
Subtract the second equation from the first: 0.5*x = -3
Divide both sides by 0.5: x = -6
Substitute this result into the other equation: y = 0.5*x = -3

Finding closest vector to point

How can i sort vectors by distance from point?
For example i have three vectors: A, B, C and the point
Example image with point and vectors
And the sorted result must be something like this: (A, C, B)
Okay, this is more of a math question, but let me explain it here anyways. Take a look at this picture:
Let's define a line segment by vector A for the start point and a for the vector running through that line segment which end at the arrows end. Same is valid for the other segments B and C respectively. The point P as coordinates as also a vector.
Now let's make linear algebra our friend, yet be programatically efficient.
:-)
At the example of segment a you can do this and with the other respectively:
With the dot product of a and AP (vector from A to P) you get the projection projA on a where the where P is closest.
If you set A+ (projA)*na (na is the the normalized a vector) you get the closest Point in the vector a of P.
Let's set dA = A+projA*na - P and with its length you get the closest distance to compare.
Instead of saving the distances, try to store and compare squared distance of dA, dB and dC and compare those instead. It will save you to compute the square root which might become very expensive.
Here is some pseudocode:
vector3 AP = P-A;
vector3 projA = a.dot(AP);
vector3 nA = a.normalized();
dA = A + projA*na - P;
dA2 = dA.x*dA.x + dA.y*dA.y + dA.z*dA.z;
-> Compare and sort them by that value
Hope it helps a bit...

SAGE - Listing points on an elliptic curve

I have a generated elliptic curve of a modulus. I want to list just a few points on it (doesn't matter what they are, I just need one or two) and I was hoping to do:
E.points()
However due to the size of the curve this generates the error:
OverflowError: range() result has too many items
I attempted to list the first four by calling it as such:
E.points()[:4]
However that generated the same error
Is there any way I can make it list just a few points? Maybe some Sage function?
Since you did not include code to reproduce your situation, I take an example curve from the Sage documentation:
sage: E = EllipticCurve(GF(101),[23,34])
Generating random points
You can repeatedly use random_element or random_point to choose points at random:
sage: E.random_point()
(99 : 92 : 1)
sage: E.random_point()
(27 : 80 : 1)
This is probably the simplest way to obtain a few arbitrary points on the curve. random_element works in many places in Sage.
Intersecting with lines
It has the defining polynomial
sage: p = E.defining_polynomial(); p
-x^3 + y^2*z - 23*x*z^2 - 34*z^3
which is homogeneous in x,y,z. One way to find some points on that curve is by intersecting it with straight lines. For example, you could intersect it with the line y=0 and use z=1 to choose representatives (thus omitting representatives at z==0) using
sage: p(y=0,z=1).univariate_polynomial().roots(multiplicities=False)
[77]
So at that point you know that (77 : 0 : 1) is a point on your curve. You can automate things, intersecting with different lines until you have reached the desired number of points:
sage: res = []
sage: y = 0
sage: while len(res) < 4:
....: for x in p(y=y,z=1).univariate_polynomial().roots(multiplicities=False):
....: res.append(E((x, y, 1)))
....: y += 1
....:
sage: res[:4]
[(77 : 0 : 1), (68 : 1 : 1), (23 : 2 : 1), (91 : 4 : 1)]
Adapting points()
You can have a look at how the points() method is implemented. Type E.points?? and you will see that it uses an internal method called _points_via_group_structure. Looking at the source of that (using E._points_via_group_structure?? or the link to the repo), you can see how that is implemented, and probably adapt it to only yield a smaller result. In particular you can see what role that range plays here, and use a smaller range instead.

Given distances between n points, how to draw a map from those relationships

I came across an interesting question in my textbook, but not further answer or details were supplied :(
Given some points, A, B, C etc
and some distance relationships between those points:
A -> B = 23
A -> C = 45
B -> A = 23
B -> C = 78
C -> A = 45
C -> B = 78
So this distance between C and A is 45 units, A and B is 23 units etc
How to draw a map or some sort of representation? Is it just a case of constraining against those rules until you converge?
Since it is only 3 points, it is a simple triangle, and you know the distances of the three sides from the table: 23, 45, and 78 "units".
So you can plot any two of the points as a straight line, then do a little bit of math to determine the angle to the third point (and you already know the distance):
// a, b, and c are the distances, C is the angle.
c² = b² + a² - 2ba cosC
Solve that and you have the angle across point C so you can plot the third point.
Edit (I originally missed that this was for N points since it was only in the subject):.
If you don't have all of the distances, then you will have to find three that do have all three legs defined to use as a starting point and plot those. After that, find another point that has distances defined to two of your existing points and calculate your new triangle with those three points and plot that one. Repeat this until you run out of points.
I think multidimensional scaling is what you want. For example, given distances between U.S. cities, you'll get something like this:
There may not be a way to perfectly satisfy your constraints in 2- or 3-D, but this will minimize the cost function.

Resources