Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I work on computer simulations and frequently use a weighted average repeatedly inside each agent (object.) I am wondering if there was maybe a more efficient way of doing it than the way I am now.
Here's some pseudocode to show how I am currently completing the task:
Parameters:
x - first value to be averaged
y - second value to be averaged
weight - percent (from 0 to 1) that weighs the values being averaged
Code:
foo = x * weight
bar = y * (1-weight)
return foo+bar
I understand that multiplication is a very lightweight operation, but given the nature of simulations (thousands or tens of thousands of agents/objects running the operation frequently) I am curious if anyone else has any ideas.
Thanks!!
You can avoid a multiplication:
x * weight + y * (1 - weight) // 2 multiplications, 1 addition, 1 subtraction
= x * weight + y - y * weight
= x * weight - y * weight + y
= (x - y) * weight + y // 1 multiplication, 1 addition, 1 subtraction
Beyond that we'd need to know a lot more about the surrounding code, the input values, how the result is used, how often the result isn't used, the accuracy/precision needed, etc.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two types of notebook where ,
10 problems could be solved in one page, in the first notebook.
12 problems could be solved in one page, in the second notebook.
For given n problems I have to use pages in such a way that no space from both notebook should be wasted ever .Taking consideration that I have to use minimum pages also .
Output should return number of pages need for solving all problem , if not passible it should return -1.
Example :
Problem count : 10
Output : 1 (one page from first notebook)
Problem Count :12
Output :1 (one page from second notebook)
Problem Count : 5
Output : -1 (Not possible)
Problem Count : 22
Output : 2(one from first notebook + one from second notebook)
Problem Count: 23
Output:-1(not possible)
How to approach this problem , thanks in advance
Let's say that the number of problems is 2k. It must be even for any combination of pages.
Then you have to find x and y such that
10x + 12y = 2k
5x + 6y = k
5(x + y) + y = k
You have to minimize x + y. Let a = x + y then,
5a + y = k
a >= y
One solution to this problem is
a' = floor(k / 5)
y' = k mod 5
This solution minimizes y, therefore for any aolution to exist, a' >= y' for this particular solution
A general solution to this equaltion would be
a = a' - t
y = y' + 5t
In the sense that y' borrows t from a'
Maximizing t would minimize a.
To solve:
a >= y
a' - t >= y' + 5t
a' - y' >= 6t
So max t is floor((a' - y')/6)
And min a is a' - t
Constant time solution! :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Given x=1000 bits and y=500 bits, what is the longest length x+y and xy?
FYI: The answer should not be 1500 for x+y and 750k, which is why I'm confused :(
If the length of x is between 0 and 1000 bits, then the value of x is between 0 and 21000−1. Similarly, 0 ≤ y < 2500.
So, 0 ≤ x + y ≤ 21000 + 2500 − 2 < 21001, so x + y has length at most 1001.
Similarly, 0 ≤ xy < 21500, xy has length at most 1500.
Think of it as given a number from 0-99, and you add a number 0-9, how many digits are you going to need at most? 3 (2+1). And if you have an number 0-9999 and you add a number 0-99, you are going to need at most 5 digits (4+1). Notice how it is the number of digits of the largest number plus one. So the answer is 1001.
Consider 2 bit numbers. In the worst case you can get:
11b + 11b = 110b
So 3 bits is enough, not 2 + 2. For the number at most N you need ceil(log(N)) bits (where log means logarithm with base 2). So if you have two numbers at most N and at most M you'll need ceil(log(N+M)) bits.
For multiplication, consider 3 bit numbers:
111b * 111b = 110001b
That's why it's also not a simple multiplication of the number of bits of the arguments. Similarly to the above for multiplication you need ceil(log(N*M)) bits.
Answer depends upon how you want to handle overflow (and how you define the functions + and *).
lets adopt the notation var:bitwidth to mean variable has bitwidth bits. That would mean you have the following declarations,
x:1000
y:500
Furthermore, we adopt the convention that these are stored in bigendian order (rightmost bit is smallest bit, leftmost is largest bit). We quickly conclude that x+y needs 1001 to handle overflow bit, that is,
z0:1000, z1:1001
z0 = x + y //overflow possible, ex: x=2^1000-1, y=1
z1 = x + y //overflow not possible
Multiplication is harder, consider a:8, b:8, what is the widest result?
a:8, b:8
a = 11111111b //= 255
b = 11111111b //= 255
a * b == 1111111000000001 //=65025
//16 bits
Seems reasonable to expect that the number of bits needed for multiplication is the sum of the bits, unless you want to just have an overflow bit for multiplication overflow.
x:1000, y:5000
z:1500
z2:1499
z = x * y //would not have overflow
z2 = x * y //could have overflow
All this said, normal microprocessors just use the width of the larger (:1000), and either drop overflow or set an overflow bit. So the answer is :1000 or :1000 plus :1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
There are n independent random variables X1,X2..Xn. Each random variable can take value of either 0 or 1. The probability that a variable Xi has a value of 1 is 1/n. What is the expected value of square of sum of X1..Xn.
This may be homework, so I'll give a few hints:
We want E((\sum_i X_i) ^2). Now show that:
E((\sum_i X_i)^2) = E(\sum_i X_i^2 + 2\sum_{1<= i < j <= n} X_i * X_j)
= n * E(X_i^2) + 2 * choose(n, 2) * E(X_i * X_j)
Now all you need is:
E(X_i^2), E(X_i * X_j)
For any i and j, since they are i.i.d.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to solve this equation with 28 variables:
y = (a1 * x1) + (a2 * x2) + .... + (a28 * x28)
1) y is known, so are a1, a2 all the way through a28.
2) x1, x2 ..... x28 are unknown variables and they are in the range of [-4, 4] with 0.1 increment.
Could somebody shed some lights on my baffled brain as to what algorithm would be the most efficient to use here?
This is equivalent to integer linear programming, though since there is only one equation with 28 simple constraints (the bounds, rather than a system of equations), you might be able to do better. In general this is going to be NP-hard (see https://en.wikipedia.org/wiki/Linear_programming#Integer_unknowns), but there are several implementations you might be able to use (see for example How to choose an integer linear programming solver?)
First of all multiply everything by 10 so you can stay in integer math. Also add sum(40*a_u) to both sides will change the range of x_i to [0,80]
Secondly there may be an exponential number of answers so your algorithm must take exponential time.
Given that there are 80^28 (approximately 2^177) possible answers - this is not possible in general.
Now if the range of x_i were [0,1] (and not [0,80]) and we add an extra term that is equal to y (and change y to 0), than the problem becomes find a subset of a set of integers that add up to zero. This is a well known NP complete problem, and it seems even easier than yours (although I don't have a clear reduction).
There may be a dynamic programming solution, but it may be too slow:
set<float> X;
X.insert(0)
for i = 1 to 28
for f = -4.0 to 4.0 step 0.1
for x in X
X.insert(x + a_i * f)
for x in X
if (x == y)
return true;
return false;
You can do better than this by passing back the feasible range (of [y + a_i*(-4.0), y + a_i*4.0]) and prune infeasible partial solutions outside those bounds.
You can program it in prolog (SICStus prolog engine and for example SPIDER IDE on Eclipse). This problem is state space searching problem. And use clpfd library (Constraint Logic Programming over Finite Domains). Then you just do one constraint, X1 to X28 will be domain variable and given constraint y #= a1*X1 + ... + a28*X28. There is also few ways to optimalize searching of state space.
/edit:
Or you can try do it in any imperative language. Also use some heuristics - for example, choose some points of execution, where you can check current result (for example, you have some tmp. sum and you had already count with 15 from 28 values. If y minus temp sum is lesser than MIN_VARIABLE_VALUE * i, where i is index and x_i belongs to remaining variables, you can safely decide, that you won´t continue, bcs. you can´t get equality). This heuristic get on my mind first. Use can also use some substitution in this. But there should be done "research" on some test data how much efficient it is.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
Is there some way to calculate the inverse factorials of real numbers?
For example - 1.5 ! = 1.32934039
Is there some way to obtain 1.5 back if I have the value 1.32934039?
I am trying
http://www.wolframalpha.com/input/?i=Gamma^(-1)[1.32934039]
but that is a fail.
Using wolframalpha.com, you can ask for
Solve[Gamma[x+1]==1.32934039,x]
As mentioned in the comments, Gamma does not have a unique inverse. True even when you are solving for a conventional factorial, e.g.
Solve[Gamma[x+1]==6,x]
yields several answers, of which one is 3.
Instead of using Gamma[] in WolframAlpha, you can also use Factorial[]:
Solve[Factorial[x]==6,x]
Solve[Factorial[x]==1.32934039,x]
David Cantrell gives a good approximation of Γ-1(n) on this page:
k = the positive zero of the digamma function, approximately 1.461632
c = Sqrt(2*pi)/e - Γ(k), approximately 0.036534
L(x) = ln((x+c)/Sqrt(2*pi))
W(x) = Lambert W function
ApproxInvGamma(x) = L(x) / W(L(x) / e) + 1/2
For integers you can do:
i = 2
n = someNum
while (n != 1):
n /= i
i += 1
return (i==1 ? i : None)
The factorial for real numbers has no inverse. You say that "each function must have an inverse". That is incorrect. Consider the constant function f(x)=0. What is f^-1(42)? For a function to be inverse it must be both an injection and a surjection.