I'm not quite certain how to go about interpreting this.
I'm solving a fairly large system of differential equations, DSolve sometimes spits out a list of replacement rules that include terms that have a #1 . I know that #1 is a placeholder for an argument, but I just have no clue where it comes from.
If I have a system of equations similar to
eqs = {
x1'[t] = a1*x1[t] + b1*y1[t]
x2'[t] = a2*x2[t] + b2*y2[t]
...
y1'[t] = c1*y1[t] + d1*x1[t]
y2'[t] = c2*y2[t] + d2*x2[t]}
DSolve[eqs,vars,t] spits out something like
x1 -> e^(-ta1)
x2 -> e^(-t)RootSum[a1a2+a3b4#1 + a3a1b2#1]
...
Obviously a little more complicated but you get the point.
Nothing in the documentation hints as to why this is occurring, And it only happens under certain permuations of parameters (e.g. when I play around with parameters in the original system it either works or doesn't)
This RootSum may be generated by Integrate, which is used by DSolve internally, like so:
In[511]:= Integrate[1/(1 + x + x^2 + x^3 + x^4), x]
Out[511]= RootSum[1 + #1 + #1^2 + #1^3 + #1^4 &,
Log[x - #1]/(1 + 2 #1 + 3 #1^2 + 4 #1^3) &]
It represents a symbolic expression that is the Sum[ Log[x-t]/(1+2*t+3 t^2+4 t^3), {t, {"roots of 1+t+t^2+t^3+t^4"}] (caution, invalid syntax intentional). You can recover the expected normal form using Normal:
In[512]:= Normal[%]
Out[512]=
Log[(-1)^(1/5) + x]/(1 - 2 (-1)^(1/5) + 3 (-1)^(2/5) - 4 (-1)^(3/5)) +
Log[-(-1)^(2/5) + x]/(
1 - 4 (-1)^(1/5) + 2 (-1)^(2/5) + 3 (-1)^(4/5)) +
Log[(-1)^(3/5) + x]/(
1 - 3 (-1)^(1/5) - 2 (-1)^(3/5) + 4 (-1)^(4/5)) +
Log[-(-1)^(4/5) + x]/(1 + 4 (-1)^(2/5) - 3 (-1)^(3/5) + 2 (-1)^(4/5))
Or using the Sum directly:
In[513]:= Sum[
Log[x - t]/(1 + 2*t + 3 t^2 + 4 t^3), {t,
t /. {ToRules[Roots[1 + t + t^2 + t^3 + t^4 == 0, t]]}}]
Out[513]=
Log[(-1)^(1/5) + x]/(1 - 2 (-1)^(1/5) + 3 (-1)^(2/5) - 4 (-1)^(3/5)) +
Log[-(-1)^(2/5) + x]/(
1 - 4 (-1)^(1/5) + 2 (-1)^(2/5) + 3 (-1)^(4/5)) +
Log[(-1)^(3/5) + x]/(
1 - 3 (-1)^(1/5) - 2 (-1)^(3/5) + 4 (-1)^(4/5)) +
Log[-(-1)^(4/5) + x]/(1 + 4 (-1)^(2/5) - 3 (-1)^(3/5) + 2 (-1)^(4/5))
In[514]:= % - %% // FullSimplify
Out[514]= 0
Related
I mean a function that accepts an array of elements and a combination as params, and returns a number that represents the index of a combination without generating every combination.
I have no preference, it can be in any programming language.
An example of code getCombinationIndex("114") and should return the index of combination 114.
[1,1,1]: 1
[2,1,1]: 2
[3,1,1]: 3
[4,1,1]: 4
[.....]
[1,1,4]: ?
Let's say you are considering combinations of k symbols from alphabet A = {a_0, a_1, ..., a_n} (i.e. with n symbols and a_i < a_j lexicographically if i < j). In your example, you have an alphabet of 4 symbols A = {1, 2, 3, 4} and combinations of k = 3 symbols.
Then, a combination c = [a_i1, a_i2, ..., a_ik] can be uniquely encoded as I(c) = i1 + n*i2 + (n^2)*i3 + ... + (n^(k-1))*ik. The indexing you're looking for is F(c) = I(c) + 1.
Let's see how it works for your example:
F([1,1,1]) = I([1,1,1]) + 1 = 0 + 4*0 + (4^2)*0 + 1 = 1
F([2,1,1]) = I([2,1,1]) + 1 = 1 + 4*0 + (4^2)*0 + 1 = 2
F([3,1,1]) = I([2,1,1]) + 1 = 2 + 4*0 + (4^2)*0 + 1 = 3
F([4,1,1]) = I([2,1,1]) + 1 = 3 + 4*0 + (4^2)*0 + 1 = 4
...
F([2,1,3]) = I([2,2,3]) + 1 = 1 + 4*1 + (4^2)*2 + 1 = 38
...
F([1,1,4]) = I([1,1,4]) + 1 = 0 + 4*0 + (4^2)*3 + 1 = 49
...
F([4,4,4]) = I([4,4,4]) + 1 = 3 + 4*3 + (4^2)*3 + 1 = 64
This problem can be seen as base conversion. You need two informations to start with and then it will be only a base conversion.
The base
In your case this is the highest number of all the items.
[4,1,1] -> 4
The desired combination
This only works for the premiss that all items can have the same maximum.
Algorithm
Reverse the order of items
Decrement every item by 1
Convert the number to base 10
Increment by 1
Example
Start: 114
Reverse: 411
Decrement: 300
Conversion:
Base 4: 300
Base 10: 3*4^2 + 0*4^1 + 0*4^0 = 24
Increment: 25
I'm trying to figure out this algorithm that accepts an input of an int and should return an output of the sum of each element in the int.
# Input -> 4321
# output -> 10 (4+3+2+1)
def sum_func(n):
# Base case
if len(str(n)) == 1:
return n
# Recursion
else:
return n%10 + sum_func(n/10)
When Trying to break apart this algorithm this is what I come up with
1st loop -> 1 + 432 = 433
2nd loop -> 2 + 43 = 45
3rd loop -> 3 + 4 = 7
4th loop -> 4 + 4 = 8
How was it able to come up with the result of 10?
Unwinding, it would look like this:
sum_func(4321)
= 1 + sum_func(432)
= 1 + 2 + sum_func(43)
= 1 + 2 + 3 + sum_func(4)
= 1 + 2 + 3 + 4
When trying to understand recursion you'll have to clearly understand what is returned.
In this case function sum_func(n) returns the sum of the digits in it's argument n.
For concrete n task is divided into last_digit_of_n + sum_func(n_without_last_digit).
For example,
sum_func(4321) =
sum_func(432) + 1 =
sum_func(43) + 2 + 1 =
sum_func(4) + 3 + 2 + 1 =
4 + 3 + 2 + 1
Hope this helps.
(As a side note, checking if n has more than one digit using str is a bad idea. Better just to check if n <= 9)
You must reach the base case before the summation occurs:
Iteration 1: 1 + sum_func(432)
Iteration 2: 1 + 2 + sum_func(43)
Iteration 3: 1 + 2 + 3 + sum_func(4) = 1 + 2 + 3 + 4 = 10
I'm wondering if someone can help me try to figure this out.
I want f(str) to take a string str of digits and return the sum of all substrings as numbers, and I want to write f as a function of itself so that I can try to solve this with memoization.
It's not jumping out at me as I stare at
Solve("1") = 1
Solve("2") = 2
Solve("12") = 12 + 1 + 2
Solve("29") = 29 + 2 + 9
Solve("129") = 129 + 12 + 29 + 1 + 2 + 9
Solve("293") = 293 + 29 + 93 + 2 + 9 + 3
Solve("1293") = 1293 + 129 + 293 + 12 + 29 + 93 + 1 + 2 + 9 + 3
Solve("2395") = 2395 + 239 + 395 + 23 + 39 + 95 + 2 + 3 + 9 + 5
Solve("12395") = 12395 + 1239 + 2395 + 123 + 239 + 395 + 12 + 23 + 39 + 95 + 1 + 2 + 3 + 9 + 5
You have to break f down into two functions.
Let N[i] be the ith digit of the input. Let T[i] be the sum of substrings of the first i-1 characters of the input. Let B[i] be the sum of suffixes of the first i characters of the input.
So if the input is "12395", then B[3] = 9+39+239+1239, and T[3] = 123+12+23+1+2+3.
The recurrence relations are:
T[0] = B[0] = 0
T[i+1] = T[i] + B[i]
B[i+1] = B[i]*10 + (i+1)*N[i]
The last line needs some explanation: the suffixes of the first i+2 characters are the suffixes of the first i+1 characters with the N[i] appended on the end, as well as the single-character string N[i]. The sum of these is (B[i]*10+N[i]*i) + N[i] which is the same as B[i]*10+N[i]*(i+1).
Also f(N) = T[len(N)] + B[len(N)].
This gives a short, linear-time, iterative solution, which you could consider to be a dynamic program:
def solve(n):
rt, rb = 0, 0
for i in xrange(len(n)):
rt, rb = rt+rb, rb*10+(i+1)*int(n[i])
return rt+rb
print solve("12395")
One way to look at this problem is to consider the contribution of each digit to the final sum.
For example, consider the digit Di at position i (from the end) in the number xn-1…xi+1Diyi-1…y0. (I used x, D, and y just to be able to talk about the digit positions.) If we look at all the substrings which contain D and sort them by the position of D from the end of the number, we'll see the following:
D
xD
xxD
…
xx…xD
Dy
xDy
xxDy
…
xx…xDy
Dyy
xDyy
xxDyy
…
xx…xDyy
and so on.
In other words, D appears in every position from 0 to i once for each prefix length from 0 to n-i-1 (inclusive), or a total of n-i times in each digit position. That means that its total contribution to the sum is D*(n-i) times the sum of the powers of 10 from 100 to 10i. (As it happens, that sum is exactly (10i+1−1)⁄9.)
That leads to a slightly simpler version of the iteration proposed by Paul Hankin:
def solve(n):
ones = 0
accum = 0
for m in range(len(n),0,-1):
ones = 10 * ones + 1
accum += m * ones * int(n[m-1])
return accum
By rearranging the sums in a different way, you can come up with this simple recursion, if you really really want a recursive solution:
# Find the sum of the digits in a number represented as a string
digitSum = lambda n: sum(map(int, n))
# Recursive solution by summing suffixes:
solve2 = lambda n: solve2(n[1:]) + (10 * int(n) - digitSum(n))/9 if n else 0
In case it's not obvious, 10*n-digitSum(n) is always divisible by 9, because:
10*n == n + 9*n == (mod 9) n mod 9 + 0
digitSum(n) mod 9 == n mod 9. (Because 10k == 1 mod n for any k.)
Therefore (10*n - digitSum(n)) mod 9 == (n - n) mod 9 == 0.
Looking at this pattern:
Solve("1") = f("1") = 1
Solve("12") = f("12") = 1 + 2 + 12 = f("1") + 2 + 12
Solve("123") = f("123") = 1 + 2 + 12 + 3 + 23 + 123 = f("12") + 3 + 23 + 123
Solve("1239") = f("1239") = 1 + 2 + 12 + 3 + 23 + 123 + 9 + 39 + 239 + 1239 = f("123") + 9 + 39 + 239 + 1239
Solve("12395") = f("12395") = 1 + 2 + 12 + 3 + 23 + 123 + 9 + 39 + 239 + 1239 + 5 + 95 + 395 + 2395 + 12395 = f("1239") + 5 + 95 + 395 + 2395 + 12395
To get the new terms, with n being the length of str, you are including the substrings made up of the 0-based index ranges of characters in str: (n-1,n-1), (n-2,n-1), (n-3,n-1), ... (n-n, n-1).
You can write a function to get the sum of the integers formed from the substring index ranges. Calling that function g(str), you can write the function recursively as f(str) = f(str.substring(0, str.length - 1)) + g(str) when str.length > 1, and the base case with str.length == 1 would just return the integer value of str. (The parameters of substring are the start index of a character in str and the length of the resulting substring.)
For the example Solve("12395"), the recursive equation f(str) = f(str.substring(0, str.length - 1)) + g(str) yields:
f("12395") =
f("1239") + g("12395") =
(f("123") + g("1239")) + g("12395") =
((f("12") + g("123")) + g("1239")) + g("12395") =
(((f("1") + g("12")) + g("123")) + g("1239")) + g("12395") =
1 + (2 + 12) + (3 + 23 + 123) + (9 + 39 + 239 + 1239) + (5 + 95 + 395 + 2395 + 12395)
I am trying to prove an equation given in the CLRS exercise book. The equation is:
Sigma k=0 to k=infinity (k-1)/2^k = 0
I solved the LHS but my answer is 1 whereas the RHS should be 0
Following is my solution:
Let's say S = k/2^k = 1/2 + 2/2^2 + 3/2^3 + 4/2^4 ....
2S = 1 + 2/2 + 3/2^2 + 4/2^3 ...
2S - S = 1 + ( 2/2 - 1/2) + (3/2^2 - 2/2^2) + (4/2^3 - 3/2^3)..
S = 1+ 1/2 + 1/2^2 + 1/2^3 + 1/2^4..
S = 2 -- eq 1
Now let's say S1 = (k-1)/2^k = 0/2 + 1/2^2 + 2/2^3 + 3/2^4...
S - S1 = 1/2 + (2/2^2 - 1/2^2) + (3/2^3 - 2/2^3) + (4/2^4 - 3/2^4)....
S - S1 = 1/2 + 1/2^2 + 1/2^3 + 1/2^4...
= 1
From eq 1
2 - S1 = 1
S1 = 1
Whereas the required RHS is 0. Is there anything wrong with my solution? Thanks..
Yes, you have issues in your solution to the problem.
While everything is correct in formulating the value of S, you have calculated the value of S1 incorrectly. You missed substituting the value for k=0 in S1. Whereas, for S, even after putting the value of k, the first term will be 0, so no effect.
Therefore,
S1 = (k-1)/2^k = -1 + 0/2 + 1/2^2 + 2/2^3 + 3/2^4...
// you missed -1 here because you started substituting values from k=1
S - S1 = -(-1) + 1/2 + (2/2^2 - 1/2^2) + (3/2^3 - 2/2^3) + (4/2^4 - 3/2^4)....
S - S1 = 1 + (1/2 + 1/2^2 + 1/2^3 + 1/2^4...)
= 1 + 1
= 2.
From eq 1
2 - S1 = 2
S1 = 0.
This was a Google question i didn't figure out was right or wrong but a 2nd opinion never hurt. but the question is "given an n length bit-string solve for the number of times "111" appeared in all possible combinations."
now i know to find total combinations is 2^n what took me trouble was figuring out the number of occurrences i did find a pattern in occurrences but who knows for sure what happens when n becomes vast.
My logical solution was
#Level (n length) # combos # strings with "111" in it
_________________ ________ _____________________________
0 0 0
1("1" or "0") 2 0
2("11","01" etc. 4 0
3 8 1("111")
4 16 3
5 32 8
6 64 20
------------------------------------Everything before this is confirmed
7 128 49
8 256 119
9 512 288
10 1000 696
etc.. i can post how i came up with the magical fairy dust but yeah
I can help you with a solution:
Call the function to calculate number of string with n bit contains 111 is f(n)
If the first bit of the string is 0, we have f(n) += f(n - 1)//0 + (n - 1 bits)
If the first bit of the string is 1, we have f(n) += f(n - 2) + f(n - 3) + 2^(n - 3)
More explanation for case first bit is 1
If the first bit is 1, we have three cases:
10 + (n - 2 bits) = f(n - 2)
110 + (n - 3 bits) = f(n - 3)
111 + (n - 3 bits) = 2^(n - 3) as we can take all combinations.
So in total f(n) = f(n - 1) + f(n - 2) + f(n - 3) + 2^(n - 3).
Apply to our example:
n = 4 -> f(4) = f(3) + f(2) + f(1) + 2^1 = 1 + 0 + 0 + 2 = 3;
n = 5 -> f(5) = f(4) + f(3) + f(2) + 2^2 = 3 + 1 + 0 + 4 = 8;
n = 6 -> f(6) = f(5) + f(4) + f(3) + 2^3 = 8 + 3 + 1 + 8 = 20;
n = 7 -> f(7) = f(6) + f(5) + f(4) + 2^4 = 20 + 8 + 3 + 16 = 47;
n = 8 -> f(8) = f(7) + f(6) + f(5) + 2^5 = 47 + 20 + 8 + 32 = 107;
n = 9 -> f(9) = f(8) + f(7) + f(6) + 2^6 = 107 + 47 + 20 + 64 = 238;