Getting function using GMP
up vote
0
down vote
favorite
I have point(x,y) and i want add another point. i write only formul, but i want getting function:
struct point {
mpz_t x;
mpz_t y;
};
point my_func(mpz_t x, mpz_t y) {
/..../
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(e, c, P);
return (x, y);
}
It seems to me that this should not be difficult, but I have been driving all day.
point gp = {x, y};
error: array must be initialized with a brace-enclosed initializer
I want only having a point (x, y) to get the next point at the output. it does not have to be a structure, I tried pair and tuple, it still does not help.
c++ func gmp
add a comment |
up vote
0
down vote
favorite
I have point(x,y) and i want add another point. i write only formul, but i want getting function:
struct point {
mpz_t x;
mpz_t y;
};
point my_func(mpz_t x, mpz_t y) {
/..../
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(e, c, P);
return (x, y);
}
It seems to me that this should not be difficult, but I have been driving all day.
point gp = {x, y};
error: array must be initialized with a brace-enclosed initializer
I want only having a point (x, y) to get the next point at the output. it does not have to be a structure, I tried pair and tuple, it still does not help.
c++ func gmp
1
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
>return (x, y);
That won't work.return {x, y}
is possible though
– papagaga
Nov 22 at 15:37
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have point(x,y) and i want add another point. i write only formul, but i want getting function:
struct point {
mpz_t x;
mpz_t y;
};
point my_func(mpz_t x, mpz_t y) {
/..../
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(e, c, P);
return (x, y);
}
It seems to me that this should not be difficult, but I have been driving all day.
point gp = {x, y};
error: array must be initialized with a brace-enclosed initializer
I want only having a point (x, y) to get the next point at the output. it does not have to be a structure, I tried pair and tuple, it still does not help.
c++ func gmp
I have point(x,y) and i want add another point. i write only formul, but i want getting function:
struct point {
mpz_t x;
mpz_t y;
};
point my_func(mpz_t x, mpz_t y) {
/..../
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(e, c, P);
return (x, y);
}
It seems to me that this should not be difficult, but I have been driving all day.
point gp = {x, y};
error: array must be initialized with a brace-enclosed initializer
I want only having a point (x, y) to get the next point at the output. it does not have to be a structure, I tried pair and tuple, it still does not help.
c++ func gmp
c++ func gmp
edited Nov 22 at 15:29
stackptr
8,20223256
8,20223256
asked Nov 22 at 14:52
vbujym
11
11
1
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
>return (x, y);
That won't work.return {x, y}
is possible though
– papagaga
Nov 22 at 15:37
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50
add a comment |
1
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
>return (x, y);
That won't work.return {x, y}
is possible though
– papagaga
Nov 22 at 15:37
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50
1
1
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
>
return (x, y);
That won't work. return {x, y}
is possible though– papagaga
Nov 22 at 15:37
>
return (x, y);
That won't work. return {x, y}
is possible though– papagaga
Nov 22 at 15:37
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
really, why bother with something if i need to just change the values of x and y
the void function by reference solved the problem, I am grateful for the useful advice
this function working and need RAM 600kb
void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y, mpz_t& P) {
mpz_t a, b, c, d, f;
mpz_inits(a, b, c, d, f, 0);
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(f, c, P);
//set x, y
mpz_set(x, d);
mpz_set(y, f);
mpz_clears(a, b, c, d, f, nullptr);
}
Don't forget to mpz_clear the variables. Also, you can simplifympz_t& x
tompz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to writeb=a-x;
and not need the init/clear for each variable.
– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
really, why bother with something if i need to just change the values of x and y
the void function by reference solved the problem, I am grateful for the useful advice
this function working and need RAM 600kb
void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y, mpz_t& P) {
mpz_t a, b, c, d, f;
mpz_inits(a, b, c, d, f, 0);
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(f, c, P);
//set x, y
mpz_set(x, d);
mpz_set(y, f);
mpz_clears(a, b, c, d, f, nullptr);
}
Don't forget to mpz_clear the variables. Also, you can simplifympz_t& x
tompz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to writeb=a-x;
and not need the init/clear for each variable.
– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
|
show 2 more comments
up vote
0
down vote
really, why bother with something if i need to just change the values of x and y
the void function by reference solved the problem, I am grateful for the useful advice
this function working and need RAM 600kb
void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y, mpz_t& P) {
mpz_t a, b, c, d, f;
mpz_inits(a, b, c, d, f, 0);
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(f, c, P);
//set x, y
mpz_set(x, d);
mpz_set(y, f);
mpz_clears(a, b, c, d, f, nullptr);
}
Don't forget to mpz_clear the variables. Also, you can simplifympz_t& x
tompz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to writeb=a-x;
and not need the init/clear for each variable.
– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
really, why bother with something if i need to just change the values of x and y
the void function by reference solved the problem, I am grateful for the useful advice
this function working and need RAM 600kb
void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y, mpz_t& P) {
mpz_t a, b, c, d, f;
mpz_inits(a, b, c, d, f, 0);
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(f, c, P);
//set x, y
mpz_set(x, d);
mpz_set(y, f);
mpz_clears(a, b, c, d, f, nullptr);
}
really, why bother with something if i need to just change the values of x and y
the void function by reference solved the problem, I am grateful for the useful advice
this function working and need RAM 600kb
void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y, mpz_t& P) {
mpz_t a, b, c, d, f;
mpz_inits(a, b, c, d, f, 0);
//lambda
mpz_sub(a, Y, y);
mpz_sub(b, X, x);
mpz_invert(c, b, P);
mpz_mul(b, a, c);
mpz_mod(f, b, P);
//point x
mpz_mul(a, f, f);
mpz_sub(b, a, x);
mpz_sub(c, b, X);
mpz_mod(d, c, P);
//point y
mpz_sub(a, x, d);
mpz_mul(b, f, a);
mpz_sub(c, b, y);
mpz_mod(f, c, P);
//set x, y
mpz_set(x, d);
mpz_set(y, f);
mpz_clears(a, b, c, d, f, nullptr);
}
edited Dec 3 at 11:47
answered Nov 24 at 8:56
vbujym
11
11
Don't forget to mpz_clear the variables. Also, you can simplifympz_t& x
tompz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to writeb=a-x;
and not need the init/clear for each variable.
– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
|
show 2 more comments
Don't forget to mpz_clear the variables. Also, you can simplifympz_t& x
tompz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to writeb=a-x;
and not need the init/clear for each variable.
– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
Don't forget to mpz_clear the variables. Also, you can simplify
mpz_t& x
to mpz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to write b=a-x;
and not need the init/clear for each variable.– Marc Glisse
Nov 24 at 22:31
Don't forget to mpz_clear the variables. Also, you can simplify
mpz_t& x
to mpz_t x
in the function arguments, because mpz_t already acts like a pointer/reference (that's why you couldn't return it). I still recommend you use a C++ wrapper, it is so much more convenient to write b=a-x;
and not need the init/clear for each variable.– Marc Glisse
Nov 24 at 22:31
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
Yes, I turned on cleaning at the end of my function, but if I run it sequentially in a loop, I see how the amount of RAM grows, as soon as the loop ends, the memory is cleared. While not yet solved this problem.
– vbujym
Nov 26 at 6:52
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
If the numbers get progressively bigger and bigger, it is normal that they take more RAM (and the iterations get slower).
– Marc Glisse
Nov 26 at 12:29
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I understand this, of course, yes but in my case I write:mpz_clears(a, b, c, d, f, NULL); and then immediately after i call mpz_out_str(stdout, 10, a); and see that these variables are not cleared!
– vbujym
Nov 26 at 14:27
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
I tried to clear each variable separately (mpz_clear(a);), but it still does not help.
– vbujym
Nov 26 at 14:33
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433499%2fgetting-function-using-gmp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why don't you use boost multiprecision? It has a nice C++ wrapper for GMP.
– The Quantum Physicist
Nov 22 at 14:54
I read that boost uses gmp and decided to use it right away. I'll try boost, but I'm sure it will take me a whole day (.
– vbujym
Nov 22 at 14:59
>
return (x, y);
That won't work.return {x, y}
is possible though– papagaga
Nov 22 at 15:37
Without going to boost, GMP itself comes with a C++ interface (header gmpxx.h, type mpz_class). You cannot return a mpz_t (or an aggregate of mpz_t), it doesn't have value semantics, either you need a C++ wrapper, or you can pass a reference/pointer to a point to the function and assign to it inside the function (then you don't need to return anything).
– Marc Glisse
Nov 22 at 15:39
Marc Glisse, thank you!!!
– vbujym
Nov 24 at 8:50