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 8 years ago.
Improve this question
I am trying to have this loop but it says its not possible. How can I create these kinds of loops?
for i in range (sposA,sposB) and for j in range(eposB+1,sposB,-1):
if tempstr[i] == ctempstr[j]:
pcount += 1
In basically any language, such a construct would be ambiguous. Are you trying to loop in two dimensions (if each index list is x and y long, are you doing x*y things total?) or in parallel (are the indices the same length, e.g. x, and paired, so you only do x things?).
If you want to loop in two dimensions, you simply nest loops:
for i in range(x):
for j in range(y):
doStuff(i, j)
If they're parallel, you can either create some functional dependence between them so you can convert index i into index j, or combine the indicies:
for i in range(x):
j = f(i)
doStuff(i, j)
or
for i, j in zip(range(x), range(y)):
doStuff(i, j)
The above stuff is in Python-pseudocode, but the control structures are broadly applicable in any imperative language (C, Python, Java...).
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 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 7 years ago.
Improve this question
I'm wondering if anyone can propose an alternate way to write this code that avoids looping? Thanks!
for j=1:Ncol-1
for i=nr:Nitr
mydataD(k,1) = j;
mydataD(k,2) = i;
mydataD(k,3) = D(j,i);
k=k+1;
end
nr=nr+1;
end
[w,T] = kruskal(mydataD);
[r,c]=find(tril(D.',-1));
mydataD=[c,r,D(sub2ind(size(D),c,r))];
The transpose and swapped row/column are so that the row numbers are sorted. This doesn't include elements whose values are zero. If you actually want the zero values we'll have to change the first line:
[r,c]=find(tril(ones(size(D),-1));
mydataD=[c,r,D(sub2ind(size(D),c,r))];
This looks to me to be an attempted replacement for sparse matrices, changing from an adjacency matrix to an adjacency list. The equivalent sparse matrix would be:
mydataD=sparse(triu(D,1));
To avoid the loop you could do something like this:
mydataD = zeros(Ncol -1, 3);
i = linspace(1, Ncol -1, Ncol -1);
j = linspace(nr,Niter, Ncol -1);
mydataD(:,1) = i;
mydataD(:,2) = j;
mydataD(:,3) = D(j,i);
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 8 years ago.
Improve this question
I'm stuck on optimising an algorithm; the algorithm works as such. Say we have an expression a+b+c+d and the algorithm was to attain every combination of numbers possible up to a number; let's say, n= 10.
So the algorithm would be outputting this;
1+1+1+1
1+1+1+2
1+1+1+3
..............
1+1+1+10
1+1+2+1
And so on, until it reaches the end which would be
10+10+10+10
However the problem I'm having is the runtime of the algorithm increases rapidly as n(max number) increases.
Are there any ways of cutting the runtime when n increases. I'm seriously stumped.
Can't post source code ATM as I'm not at home, but it was done in c++.
For the combination part, if "a+b+c+d" equals "a+b+d+c", then you can save some time by write the loop as
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j {
// etc.
}
}
but O(n^4) is needed.
Otherwise, if the problem is exactly "attain every combination of numbers possible up to a number", there should be a closed form solution from 4 to 4*n.
you don't need to attain every combination. just calc the result. Or I misunderstanding the question.
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 11 years ago.
Improve this question
i am wondering if it is possible to simplify:
T*V + V*T // V = V^(t) symmetric
where both operands are matrixes
I don't think this is possible due to the following considerations:
If we multiply two matrices A and T, where A is symmetric (i.e. A(i,j) = A(j,i)), we have the following:
For A*T we have that the item in row z and column s is computed as:
__n__
\
/ A(z,i)*T(i,s)
-----
i=1
For the other way around, T*A, we get for row z, column s:
__n__ __n__
\ \
/ T(z,i)*A(i,s) = / T(z,i)*A(s,i)
----- -----
i=1 i=1
So, as long as we do not know anything about the entries T(i,j) in T, I think we can not say how these sums relate to each other.
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.