How to compute a floating point modulo in Racket? [flmod] - racket

The standard modulo in Racket doesn't work with floating points.
How do I compute modulo with non-integer arguments (flmod)?

In some languages the modulo operator also works for non-integer arguments.
In Racket the modulo operator is for integer arguments only,
In the case you need one that works with floating points, use
the following definition:
(define (flmod x m)
(- x (* (floor (/ x m)) m))
There is no standard definition for such an operation (different programming
languages interprets this operation differently), so check that
the function above matches you expectations first.

Related

how to enforce positive constraints in cvx?

I have a simple CVX program, where two variables are:
variable A(n, n)
variable D(n, N)
I learned from here:
https://mycourses.aalto.fi/pluginfile.php/1612433/mod_resource/content/1/lec06.pdf
that I think adding the constraints A >= 0 (and even possibly D >= 0) will not enforce A to be all of positive values, but rather for A to be a positive definite.
How do I enforce instead for all elements of A to be positive (and similar for D)?
I am using Matlab.

What's a floating-point operation and how to count them (in MATLAB)?

I have an assignment where I basically need to count the number of floating point operations in a simple program, which involves a loop, a matrix, and operations such as *, + and ^.
From my understanding, a floating-point operation is an operation that involves floating-point numbers, and we may be interested in counting these operations because I think they may be more expensive for the computer. If you want to add more details to this part, it would be nice.
My problem is that I've no idea of knowing exactly which operations involve floating-point numbers, unless I use functions, such as isfloat. In that case, would just one of the numbers in the operation be necessary to be floating-point to the operation be considered a floating-point operation, right? If not, why? Can you add more details on this?
For example, suppose I've the following simple function:
function [r, n] = naive(c, x)
% c is the vector of coefficients of the polynomial
% The coeffiecients should be given as follows
% c(1) = coefficient of x^0 (or 1).
% c(length(c)) = coefficient of the largest power of x
% x is the point to evaluate the polynomial at
% r is the result of the evaluation
% (Assumes that the entries are integers)
r = c(1);
n = 0;
for i=2:length(c)
r = r + c(i) * x^(i - 1);
n = n + 2 + (i - 1);
end
end
which basically calculates a normal polynomial evaluated at x given the coefficients in a vector c.
As you can see from the code, n is actually keeping track of floating-point operations. But actually, I'm counting every mathematical operation (except the assignment) as a floating-point operation, but this of course might not be right, or is it? If yes or no, why?
Both the coefficients and c might be floating-point numbers. So, instead of counting every operation as a floating point operation, should we first check with for example isfloat if the numbers are floating point, and only then increment n?
Note, I'm aware of the function flops, which, from what I understood, it should count the floating-point operations, but it's deprecated, and mostly I would like to learn better these concepts, and therefore try to count them manually.
Thanks for any help!

Racket: is it possible to set surface3d steps differently for two axes?

I am trying to create a 3D plot with racket's plot library.
Ho do I make surface3d evaluate my function only in certain points? I mean, I have an function defined only on integers, but surface3d tries to evaluate it somewhere in between them, seemingly according to samples parameter. Is it possible to set that parameter differently for two axes, so that the function would be evaluated only where it is defined, or should I better use some other function for plotting?
If the problem is that surface3d evaluates the function in non-integer points, then
you can define a function that uses the values of the nearby integer points to
calculate an value for points inside the square.
If your functions is called f, then an first attempt is:
(define (g x y)
(f (floor x) (floor y)))
That will give you "stairs".
If you want a smoother appoach you can look at bilinear interpolation.
Bilinear Interpolation - Wikipedia

TI Nspire BASIC - Extract coefficients from Equation

I intend to write a function in TI-BASIC on my CAS calculator that takes a mathematical expression (a quadratic specifically) as a parameter and 'completes the square'.
To do this, I need to compute with the coefficients of the variable 'x' in the argument.
Is there a way to 'extract' the coefficients of x from the argument?
(A friend sensibly suggested to pass each coefficient as different arguments,
which would work, but seems messy and unnecessary).
Thanks!
Beg your pardon,
There's a lovely polyCoeffs() function
ListOfCoef = polyCoeffs(quadratic)

How to calculate "modular multiplicative inverse" when the denominator is not co-prime with m?

I need to calculate (a/b) mod m where a and b are very large numbers.
What I am trying to do is to calculate (a mod m) * (x mod m), where x is the modular inverse of b.
I tried using Extended Euclidean algorithm, but what to do when b and m are not co-prime?
It is specifically mentioned that b and m need to be co-prime.
I tried using the code here, and realized that for example:
3 * x mod 12 is not at all possible for any value of x, it does not exist!
What should I do? Can the algorithm be modified somehow?
Yep, you are in trouble. x has no solution in b*x = 1 mod m if b and m have a common divisor. Similarly, in your original problem a/b = y mod m, you are looking for y such that a=by mod m. If a is divisible by gcd(b,m), then you can divide out by that factor and solve for y. If not, then there is no y that can solve the equation (i.e. a/b mod m is not defined).
The reason that b and m have to be coprime is because of the Chinese Remainder Theorem. Basically the problem:
3 * x mod 12
Can be thought of as a compound problem involving
3*x mod 3 and 3*x mod 4 = 2^2
Now if b is not coprime to 12, this is like trying to divide by zero. Thus the answer doesn't exist!
This is due to field theory in abstract algebra. A field is basically a set which has addition, subtraction, multiplication, and division well-defined. A finite field is always of the form GF(p^n), where p is prime and n is a positive integer, and the operations are addition and multiplication modulo p^n. Now, 12 is not a prime power, so your ring is not a field. Thus this problem can't be solved for any b which is not coprime to m.
Check this: http://www.math.harvard.edu/~sarah/magic/topics/division
It might help.
It explains methods of modular division.