Is it possible to do a "+=" with GMP Floating-point Functions like this?
mpf_add (op1, op1, op2);
or must the three arguments always be different (requiring the use of a temp variable)?
(Same question goes for multiplication, subtraction, and division, although I'm sure the answer is the same for all four cases.)
The GMP manual states:
GMP lets you use the same variable for both input and output in one
call.
I have done so with a variety of GMP functions and it has always worked properly but I don't know if I've ever done it with the mpf functions. I generally use the MPFR library and it states that you can use the same variable for input and output, too.
Related
Both seem to draw samples from the standard normal distribution, just that standard_normal takes arguments for size and randn takes arguments for dimensions. I'm still new to this so can someone tell me if their uses will be different? Will the results they give be different?
As documented in randn:
This is a convenience function. If you want an interface that takes a tuple as the first argument, use numpy.random.standard_normal instead.
1.17 documentation is even more explicit (note randn has been moved in that version):
This is a convenience function for users porting code from Matlab, and wraps numpy.random.standard_normal.
I'm using gmp (big integer library)
I have a value b I need to check whether first m-bit of it, is zero.
I know if I convert b to string I can do checking, but it's not efficient.
Question 1: I need to know whether the library has a function that returns first m-bit of a big integer.
To encode a big integer c, I convert it to string and then concatenate it with m zeros, e.g. imagine c in binary is 11, then I encode as: 1100000000
Question 2: I need to know whether I can do encoding faster using the library's function
The GMP library provides plenty of bit manipulation primitives. Have a look at the Logical and Bit Manipulation Functions chapter from the documentation.
For your first question, I think you want to use mpz_tstbit. For your second question, it sounds like you're trying to perform some bit shifting, which can be done via the mpz_mul_2exp and the mpz_*_*_2exp functions.
I saw this question and it got me wondering.
Ignoring the fact that pretty much all languages have to be backwards compatible, is there any reason we cannot use operators as both keywords and functions, depending on if it's immediately followed by a parenthesis? Would it make the grammar harder?
I'm thinking mostly of python, but also C-like languages.
Perl does something very similar to this, and the results are sometimes surprising. You'll find warnings about this in many Perl texts; for example, this one comes from the standard distributed Perl documentation (man perlfunc):
Any function in the list below may be used either with or without parentheses around its arguments. (The syntax descriptions omit the parentheses.) If you use parentheses, the simple but occasionally surprising rule is this: It looks like a function, therefore it is a function, and precedence doesn't matter. Otherwise it's a list operator or unary operator, and precedence does matter. Whitespace between the function and left parenthesis doesn't count, so sometimes you need to be careful:
print 1+2+4; # Prints 7.
print(1+2) + 4; # Prints 3.
print (1+2)+4; # Also prints 3!
print +(1+2)+4; # Prints 7.
print ((1+2)+4); # Prints 7.
An even more surprising case, which often bites newcomers:
print
(a % 7 == 0 || a % 7 == 1) ? "good" : "bad";
will print 0 or 1.
In short, it depends on your theory of parsing. Many people believe that parsing should be precise and predictable, even when that results in surprising parses (as in the Python example in the linked question, or even more famously, C++'s most vexing parse). Others lean towards Perl's "Do What I Mean" philosophy, even though the result -- as above -- is sometimes rather different from what the programmer actually meant.
C, C++ and Python all tend towards the "precise and predictable" philosophy, and they are unlikely to change now.
Depending on the language, not() is not defined. If not() is not defined in some language, you can not use it. Why not() is not defined in some language? Because creator of that language probably had not need this type of language construction. Because it is better to let things be simpler.
I've gotten used to utilizing the numeric_limits part of the C++ STL for initializing numeric types (int,float, etc.) to their largest possible value.
I.e. int i=numeric_limits::max()
Is there an equivalent to this in objective-c? I've seen using INT_MAX and FLT_MAX in google searches, but it seems like there should be a better way.
There is:
NSIntegerMax, NSIntegerMin, CGFLOAT_MAX etc.
These are sufficient for getting the numeric limits.
I introduced myself to the GMP library for high precision arithmetic recently. It seems easy enough to use but in my first program I am running into practical problems. How are expressions to be evaluated. For instance, if I have "1+8*z^2" and z is a mpz_t "large integer" variable, how am I to quickly evaluate this? (I have larger expressions in the program that I am writing.) Currently, I am doing every single operation manually and storing the results in temporary variables like this for the "1+8*z^2" expression:
1) first do mpt_mul(z,z,z) to square z
2) then define an mpz_t variable called "eight" with the value 8.
3) multiply the result from step one by this 8 and store in temp variable.
4) define mpz_t variable called "one" with value 1.
5) add this to the result in step 3 to find final answer.
Is this what I am supposed to be doing? Or is there a better way? It would really help if there was a user's manual for GMP to get people started but there's only the reference manual.
GMP comes with a C++ class interface which provides a more straightforward way of expressing arithmetic expressions. This interface uses C++ operator overloading to allow you to write:
mpz_class z;
1 + 8 * z**2
This is, of course, assuming you're using C++. If you are using C only, you may need to use the C interface to GMP which does not provide operator overloading.
Turns out that there's an unsupported expression parser distributed with GMP in a "expr" subdirectory. It's not part of GMP proper and is subject to change but it is discussed in a README file in that directory. It isn't guaranteed to do the calculation in the fastest way possible, so buyer beware.
So the user must manually evaluate all expressions when using GMP unless they wish to use this library or make their own expression parser.