I started getting into spline principles to use them in my studies. However, I got stuck in programming the code for Wikipedia [example] . The final graph doesn't correspond to the Wiki's example. I get two curves that are not connected, despite the introduction of the parameter t(x):
Spline segments
Would you mind helping me in finding the cause of the error? Thanks.
import numpy as np
import matplotlib.pyplot as plt
x0, y0, x1, y1, x2, y2 = -1, 0.5, 0, 0, 3, 3
def spline(x0, y0, x1, y1, x2, y2):
a11 = 2 / (x1 - x0)
a12 = 1 / (x1 - x0)
a13 = 0
a21 = a12
a22 = 2 * (1 / (x1 - x0) + 1 / (x2 - x1))
a23 = 1 / (x2 - x1)
a31 = a13
a32 = a23
a33 = 2 / (x2 - x1)
b1 = 3 * ((y1 - y0) / (x1 - x0) ** 2)
b2 = 3 * ((y1 - y0) / (x1 - x0) ** 2 + (y2 - y1) / (x2 - x1) ** 2)
b3 = 3 * ((y2 - y1) / (x2 - x1) ** 2)
A = np.array([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])
L = np.array([[-b1], [-b2], [-b3]])
AT = np.transpose(A)
ATA = np.matmul(AT, A)
invATA = np.linalg.inv(ATA)
ATL = np.matmul(AT, L)
X = - np.matmul(invATA, ATL)
[k0, k1, k2] = X
a1 = k0 * (x1 - x0) - (y1 - y0)
b1 = -k1 * (x1 - x0) + (y1 - y0)
a2 = k1 * (x2 - x1) - (y2 - y1)
b2 = -k2 * (x2 - x1) + (y2 - y1)
return a1[0], b1[0], a2[0], b2[0]
a1, b1, a2, b2 = spline(-1, 0.5, 0, 0, 3, 3)
def q1(t):
return (1 - t) * y0 + t * y1 + t *(1 - t) * ((1 - t) * a1 + t * b1)
def q2(t):
return (1 - t) * y1 + t * y2 + t *(1 - t) * ((1 - t) * a2 + t * b2)
x_list = []
y1_list = []
y2_list = []
for x in range(1000):
t = (x/1000 - x1) / (x2 - x1)
x_list.append(x)
y1_list.append(q1(t))
y2_list.append(q2(t))
plt.scatter(x_list, y1_list)
plt.scatter(x_list, y2_list)
I found it! The key was the proper x-range corresponding to the 3 given points.
import numpy as np
import matplotlib.pyplot as plt
x0, y0, x1, y1, x2, y2 = -1, 0.5, 0, 0, 3, 3
def spline(x0, y0, x1, y1, x2, y2):
a11 = 2 / (x1 - x0)
a12 = 1 / (x1 - x0)
a13 = 0
a21 = a12
a22 = 2 * (1 / (x1 - x0) + 1 / (x2 - x1))
a23 = 1 / (x2 - x1)
a31 = a13
a32 = a23
a33 = 2 / (x2 - x1)
b1 = 3 * ((y1 - y0) / (x1 - x0) ** 2)
b2 = 3 * ((y1 - y0) / (x1 - x0) ** 2 + (y2 - y1) / (x2 - x1) ** 2)
b3 = 3 * ((y2 - y1) / (x2 - x1) ** 2)
A = np.array([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])
L = np.array([[-b1], [-b2], [-b3]])
AT = np.transpose(A)
ATA = np.matmul(AT, A)
invATA = np.linalg.inv(ATA)
ATL = np.matmul(AT, L)
X = - np.matmul(invATA, ATL)
[k0, k1, k2] = X
a1 = k0 * (x1 - x0) - (y1 - y0)
b1 = -k1 * (x1 - x0) + (y1 - y0)
a2 = k1 * (x2 - x1) - (y2 - y1)
b2 = -k2 * (x2 - x1) + (y2 - y1)
return a1[0], b1[0], a2[0], b2[0]
a1, b1, a2, b2 = spline(-1, 0.5, 0, 0, 3, 3)
def q1(t):
return (1 - t) * y0 + t * y1 + t *(1 - t) * ((1 - t) * a1 + t * b1)
def q2(t):
return (1 - t) * y1 + t * y2 + t *(1 - t) * ((1 - t) * a2 + t * b2)
x1_list = []
x2_list = []
y1_list = []
y2_list = []
for x in range(-1000, 0, 1):
t = (x/1000 - x0) / (x1 - x0)
x1_list.append(x)
y1_list.append(q1(t))
for x in range(3000):
t = (x/1000 - x1) / (x2 - x1)
x2_list.append(x)
y2_list.append(q2(t))
plt.scatter(x1_list, y1_list)
plt.scatter(x2_list, y2_list)
Im very new to haskell and I have to define the elliptic curve functions from the RFC6090 Definition in Haskell. (RFC6090: https://www.rfc-editor.org/rfc/rfc6090#section-3)
For that I have to implement a function called ecAdd.
ecAdd returns values from the type ECPoint. I defined them but Im not sure why the values x1 and x2 are not recognized. I get the following error for every section where x1,x2,y1 or y2 appears.
error:
• Couldn't match expected type ‘(Double -> Double) -> Fq -> Fq’
with actual type ‘galois-field-1.0.0:Data.Field.Galois.Prime.Prime
Q’
• The function ‘y2 - y1’ is applied to two arguments,
but its type ‘galois-field-1.0.0:Data.Field.Galois.Prime.Prime Q’
has none
In the first argument of ‘(^)’, namely
‘((y2 - y1) recip (x2 - x1))’
In the first argument of ‘(-)’, namely
‘((y2 - y1) recip (x2 - x1)) ^ 2’
|
64 | x3 = ((y2 - y1) recip (x2 - x1))^2 - x1 - x2
Here is my function code
ecAdd
:: ECPoint -- ^ Punkt P
-> ECPoint -- ^ Punkt Q
-> ECPoint -- ^ Ergebnis P + Q
ecAdd q O = q
ecAdd O p = p
ecAdd (A x1 y1) (A x2 y2) =
if (A x1 y1) /= (A x2 y2)
then
if x1 == x2
then O
else let x3 = ((y2-y1) recip (x2-x1))^2 - x1 - x2
y3 = (x1-x3)*(y2-y1) recip (x2-x1) - y1 in A x3 y3
else
if y1 == 0
then O
else let x4 = ((3*x1^2 + P256K1._a) recip (2*y1))^2 - 2*x1
y4 = (x1-x3)*(3*x1^2 + P256K1._a) recip (2*y1) - y1
in A x4 y4
I have 4 points and angle (as shown on the picture). How to get new point values for rotated object?
picture(x/y axis inverted, mistake. Vertical should be Y, horizontal - X)
At first, get coordinates relative to the rotation origin (x0, y0)
x' = x1 - x0
y' = y1 - y0
Then rotate
x'' = x' * Cos(Fi) - y' * Sin(Fi)
y'' = x' * Sin(Fi) + y' * Cos(Fi)
And now shift coordinates back
x_r = x'' + x0
y_r = y'' + y0
I'm trying to calculate the determinant and I have the det function defined as below:
det :: (Double,Double) -> (Double,Double) -> Double
det _ = 0.0
det [] = error "its Empty!"
det [x] = error "contains only one point!"
det (x1, y1) (x2, y2) = (x1*y2 + x2*y1)
i get the following error
Equations for ‘det’ have different numbers of arguments
PolygonArea.hs:11:1-11
PolygonArea.hs:14:1-39
Failed, modules loaded: none.
First of all, the type (Double, Double) is always a pair of two Doubles, never an empty or a singleton list. Second, like the error message says, your cases for det need to have the same number of arguments, but one are given for the first three, and two for the fourth.
Fixing all these, you get
det :: (Double, Double) -> (Double, Double) -> Double
det (x1, y1) (x2, y2) = (x1 * y2 + x2 * y1)
Can you see what is wrong with this:
(define (box d x1 y1 x2 y2) (
(graphics-draw-line d x1 y1 x1 y2)
(graphics-draw-line d x1 y2 x2 y2)
(graphics-draw-line d x2 y2 x2 y1)
(graphics-draw-line d x2 y1 x1 y1) ))
When I call it like this:
( begin
(define w (make-graphics-device 'x))
(box w .10 .10 .20 .20) )
I get an errer:
;The object #!unspecific is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
2 error>
This works:
(begin
(define w (make-graphics-device 'x))
(graphics-draw-line w .1 .1 .1 .2)
(graphics-draw-line w .1 .2 .2 .2)
(graphics-draw-line w .2 .2 .2 .1)
(graphics-draw-line w .2 .1 .1 .1) )
I can't see the difference!
Don't just group expressions with ()s -- that will try to use the result of the first as a function, but the value is #!unspecific -- definitely not a function.
Use this:
(define (box d x1 y1 x2 y2)
(graphics-draw-line d x1 y1 x1 y2)
(graphics-draw-line d x1 y2 x2 y2)
(graphics-draw-line d x2 y2 x2 y1)
(graphics-draw-line d x2 y1 x1 y1))