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.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Is matrix multiplication speed faster for sparse matrixes than dense matrixes?
To give a simplified example,
does
"[[0,0],[0,0]] multiplies [[1,1],[1,1]]"
faster than
"[[256,256],[256,256]] multiplies [[1,1],[1,1]]" ?
The machine code algorithm to do a multiplication goes like this:
int mul(int a,int b)
{
int result = 0;
bit sign = sign(a) ^ sign(b);
a = abs(a); b = abs(b);
while (b != 0)
{
b = b>>1; // shift b right, bit0 into carry
if (carrySet()) result += a;
a = a<<1; // shift a left
// note: checks for overflow being left out
}
return (sign==0 ? sum : -sum);
}
You'll easily see that the more bits are set in the right operand, the more computations are necessary to sum up the left operand.
So, provided that your matrix multiplication boils down to machine code multiplications like this, a sparse matrix will multiply significantly faster than a dense matrix.
The question I cannot answer here is if the FPU will do this in a more efficient manner. You'll want to read some specs here. But even if a FPU (or GPU) is doing some sort of tweaking, I doubt the basic multiplication grinding loop looks very much different (interested in comments about this.)
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How can I generate a matrix in Matlab that has 5 rows and some specific number of columns and the elements may only be binary numbers and the column sum has to be less or equal to 3?
Some possibilites without loops:
Using strings:
D = 5;
S = 3;
numbers = str2mat(dec2bin(0:2^D-1))-'0';
numbers = numbers(sum(numbers,2)<=S,:);
Using combinatorial numbers, one line:
numbers = [zeros(1,D); cell2mat(arrayfun(#(s) fliplr(full(sparse((1:nchoosek(D,s)).'*ones(1,s), nchoosek(1:D,s), 1))), 0:S, 'uni', 0).')];
How about this: The maximum binary number, that you can represent by 5bit is 2^5-1 = 31 and skip through these to find the ones with sum of digits <= 3.
Something like
n = 1:1:31;
for ii = 1:length(ii)
bin = dec2bin(ii)
digitSum = 0
for d = 1:length(bin)
digitSum = digitSum + str2num(bin(d))
end
if (digitSum <= 3)
%store results
end
end
Here is a vecotorized solution to provide all occurences efficiently:
Bstr =dec2bin(1:31);
Bstr(sum(dec2bin(0:31),2)<=sum('00111'),:)=='1'
Inspired by the solution of #pyStarter
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
I was using this formula to calculate last m digits of 2^n.
pow=2+(n-m)%(4*5^(m-1))
ans =(2^pow)%(10^m)**
But this is not working for n=2009 and m=3.
Suggest any error in my calculation or a better formula if there is.
I don't understand what your formula is doing, but the simplest way is to calculate (2^2009)%(10^m) . Here is a pseudo code to find (x^y)%mod in O(log y). Put x=2, y=2009 and mod=10^m
power(x,y)
{
if( y == 0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return (temp*temp)%mod
else
return ((x*temp%mod)*temp)%mod
}
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.