Replacing modulus with multiplication - math

I would like to compute x % y (i.e. x mod y), where x, y are unsigned 64-bit integers and y is a constant.
Is it true that it is possible to replace the modulus operation with some combination of multiplication, addition, shift and some well chosen constants?
How to derive such sequence of arithmentic operations and constants?
If it is not possible in general case of x and y, would it work for some reasonable subset of inputs, say if y was odd?

Related

julia : setting a variable in anonymous function so that it is set forever and you can evaluate in other variables only

So for demonstration, lets say I have a function f(x,y), where y is going to be fixed for all evaluations of f.
my_func = x -> f(x,y)
My question is I believe that when you evaluate my_func(x) it will rerun f(x,y) again and again for every x you evaluate it at. I don't want to have to reconstruct f every time I have a new x, because y is fixed. Is there any way that I can make one evaluation of f(x,y) happen for a fixed y and x is not determined yet, i.e make a g(x)= f(x,y) (because y is fixed)?
So then I can just have
my_func2 = x-> g(x)
so that I am just evaluating it for some given x.
Thank you.
I'm not 100% sure I understand what you mean. But if you do g(x) = f(x, y), and you make sure y is a const, then you will not pay any penalty relative to just evaluating f(x, y), and indeed g(x) may be even simpler since the compiler knows y is a constant. Julia will spend no extra time "reconstructing f".
If you're asking "how do I prevent spending time calculating f(x, y) for repeated values of x and y", the answer is to use memoization, i.e. caching previous computations.

How to combine multiple objectives for optimization?

I don't know why this is so hard for me to figure out.
For example, I have two functions, f(x, y) and g(x, y). I want to find the values of x and y such that:
f(x, y) is at a target value (minimize the difference from the target)
g(x, y) is minimized (can be negative, doesn't stop at 0)
x and y are bounded (so g's minimum doesn't necessarily have a gradient of 0)
So if I were just finding a solution for f, I could minimize abs(f(x, y) - target), for instance, and it will hit zero when it's found a solution. But there are multiple such solutions and I also want to find the one that minimizes g.
So how do I combine these two functions into a single expression that I can then minimize (using a Newton-like method)?
My first attempt was 100*abs(f(x, y) - target) + g(x, y) to strongly emphasize hitting the target first, and it worked for some cases of the target value, but failed for others, since g(x, y) could go so negative that it dominated the combination and the optimizer stopped caring about f. How do I guarantee that f hitting the target is always dominant?
Are there general rules for how to combine multiple objectives into a single objective?
There is a rich literature about multi-objective optimization. Two popular methods are weighted objective and a lexicographic approach.
A weighted objective could be designed as:
min w1 * [f-target]^2 + w2 * g
for some weights w1, w2 >= 0. Often we have w1+w2=1 so we can also write:
min w1 * [f-target]^2 + (1-w1) * g
Set w1 to a larger value than w2 to put emphasis on the f objective.
The lexicographic method assumes an ordering of objectives. It can look like:
Solve with first objective z = min [f-target]^2. Let z* be the optimal objective.
Solve with the second objective while staying close to z*:
min g subject to [f-target]^2-z* <= tolerance
To measure the deviation between the target and f I used a quadratic function here. You can also use an absolute value.
Since you cannot exactly get f(x,y)-target to be zero, you have to accept some amount of error. I will use the relative error r = abs((f(x, y) - target)/target).
A function which grows extremely rapidly with r should do the trick.
exp(r/epsilon) + g(x, y)
If I choose epsilon = 1e-10, then I know r has to be less than 1e-7, because exp(1000) is an enormous number, but when r is small, like r = 1e-12, then the exponential changes very slowly, and g(x,y) will be the dominant term. You can even take it a step further and calculate how close x and y are to their true value, but its usually just easier to adjust the parameter until you get what you need.

People - Apple Puzzle [Inspired by client-puzzle protocol]

I am learning a client-puzzle protocol and i have a question about finding the possibility of a solution. Instead of going into the dry protocol facts, here is a scenario:
Lets say i have x people and I have y apples:
Each person must have at least 1 apple
Each person can have at most z apples.
Is there a formula to calculate the number of scenarios?
Example:
4 people [x], 6 apples [y], 15 MAX apples [z]
No. of scenarios calculated by hand: 10.
If my number is very huge, I hope to calculate it using a formula.
Thank you for any help.
Your problem is equivalent to "finds the number of ways you can get x by adding together z numbers, each of which lies between min and max." Sample Python implementation:
def possible_sums(x, z, min, max):
if min*z > x or max*z < x:
return 0
if z == 1:
if x >= min and x <= max:
return 1
else:
return 0
total = 0
#iterate from min, up to and including max
for i in range(min, max+1):
total += possible_sums(x-i, z-1, min, max)
return total
print possible_sums(6, 4, 1, 15)
Result:
10
This function can become quite expensive when called with large numbers, but runtime can be improved with memoization. How this can be accomplished depends on the language, but the conventional Python approach is to store previously calculated values in a dictionary.
def memoize(fn):
results = {}
def f(*args):
if args not in results:
results[args] = fn(*args)
return results[args]
return f
#memoize
def possible_sums(x, z, min, max):
#rest of code goes here
Now print possible_sums(60, 40, 1, 150), which would have taken a very long time to calculate, returns 2794563003870330 in an instant.
There are ways to do this mathematically. It is similar to asking how many ways there are to roll a total of 10 on 3 6-sided dice (x=3, y=10, z=6). You can implement this in a few different ways.
One approach is to use inclusion-exclusion. The number of ways to write y as a sum of x positive numbers with no maximum is y-1 choose x-1 by the stars-and-bars argument. You can calculate the number of ways to write y as a sum of x positive numbers so that a particular set of s of them are at least z+1: 0 if y-x-sz is negative, and y-1-s z choose x-1 if it is nonnegative. Then you can use inclusion-exclusion to write the count as the sum over nonnegative values of s so that y-x-sz is nonnegative of (-1)^s (x choose s)(y-1-sz choose x-1).
You can use generating functions. You can let powers of some variable, say t, hold the total, and the coefficients say how many combinations there are with that total. Then you are asking for the coefficient of t^y in (t+t^2+...+t^z)^x. You can compute this in a few ways.
One approach is with dynamic programming, computing coefficients of (t+t^2+...+t^z)^k for k up to x. The naive approach is probably fast enough: You can compute this for k=1, 2, 3, ..., x. It is a bit faster to use something like repeated squaring, e.g., to compute the 87th power, you could expand 87 in binary as 64+16+4+2+1=0b1010111 (written as a binary literal). You could compute the 1st, 2nd, 4th, 16th, and 64th powers by squaring and multiply these, or you could compute the 0b1, 0b10, 0b101, 0b1010, 0b10101, 0b101011, and 0b1010111 powers by squaring and multiplying to save a little space.
Another approach is to use the binomial theorem twice.
(t+t^2+...+t^z)^x = t^x ((t^z-1)/(t-1))^x
= t^x (t^z-1)^x (t-1)^-x.
The binomial theorem with exponent x lets us rewrite (t^z-1)^x as a sum of (-1)^s t^(z(x-s))(x choose s) where s ranges from 0 to x. It also lets us rewrite (t-1)^-x as an infinite sum of (r+x-1 choose x-1)t^r over nonnegative r. Then we can pick out the finite set of terms which contribute to the coefficient of t^y (r = y-x-sz), and we get the same sum as by inclusion-exclusion above.
For example, suppose we have x=1000, y=1100, z=30. The value is
=1.29 x 10^144.

scilab plotting factorial; first trying to correct the equation?

I'm trying to execute this equation in scilab; however, I'm getting error: 59 of function %s_pow called ... even though I define x.
n=0:1:3;
x=[0:0.1:2];
z = factorial(3); w = factorial(n);u = factorial(3-n);
y = z /(w.*u);
t = y.*x^n*(1-x)^(3-n)
(at this point I haven't added in the plot command, although I would assume it's plot(t)?)
Thanks for any input.
The power x^n and (1-x)^(3-n) on the last line both cause the problem, because x and n are matrices and they are not the same size.
As mentioned in the documentation the power operation can only be performed between:
(A:square)^(b:scalar) If A is a square matrix and b is a scalar then A^b is the matrix A to the power b.
(A:matrix).^(b:scalar) If b is a scalar and A a matrix then A.^b is
the matrix formed by the element of A to the power b (elementwise
power). If A is a vector and b is a scalar then A^b and A.^b performs
the same operation (i.e elementwise power).
(A:scalar).^(b:matrix) If A is a scalar and b is a matrix (or
vector) A^b and A.^b are the matrices (or vectors) formed by
a^(b(i,j)).
(A:matrix).^(b:matrix) If A and b are vectors (matrices) of the same
size A.^b is the A(i)^b(i) vector (A(i,j)^b(i,j) matrix).

Reverse Bivariate Interpolation

I am given a table, indexed on 2 dimensions (x,y), with values(not necessarily ordered, though I think it isn't a horrendously unsafe assumption) z given, such that f(x, y) = z
So given an x and y, I interpolate to find a z value. Now given an x value(or y I suppose, not really important) and a z value, I need to find that y value that corresponds to the data. Is it possible to do this without the knowledge of an ordering in the z values of the table? If there is an ordering to the z values of the table is it possible? In my head, given ordering, it should be possible to find a unique solution, but I don't know how I can do it if I am not given ordering.
Could you post some or preferably all of the data? Assuming it is linear and continuous, we have n copies of ax +by = z. Let's say x=3 and z=4, we have 3 unknowns which we can put in a matrix with n rows and 3 columns. The first row would look like 3 b 4 since we are treating a, y and z as unknowns. Now try echelon row reduction. More specifically, do row1 - row2 (now don't do row2 - row1), row1-row3, row1-row4... row2-row3, row2-row4... there should be nchoose2 of these permutations. If there is a solution then each permutation will be in the form qia=qjz (the a and z won't be there of course) where qi and qj are the known numbers and q,i and j are constant.

Resources