Unexpected modulo behavior - excel

In excel, I have this formula:
MOD(-10 + 9, 12) + 1
And the expected result is 12
However, in PowerQuery the same formula:
Number.Mod(-10 + 9, 12) + 1
Results in 0
The strange thing is that for other numbers ( -1 ) I get the same result in both systems..
I expect this to have something to do with the nature of MOD, and how I'm using negative numbers.. But I would still like to know which is 'correct'..

I found the answer here:
https://www.youtube.com/watch?v=K4ImPRsi3vg&ab_channel=ExcelIsFun
MOD(n, d) = n - d * INT(n/d)
Number.Mod(n, d) = n - d * TRUNC(n/d)
They are calculated in different ways.

Related

String manipulation with dynamic programming

I have a problem where I have a string of length N, where (1 ≤ N ≤ 10^5). This string will only have lower case letters.
We have to rewrite the string so that it has a series of "streaks", where the same letter is included at least K (1 ≤ K ≤ N) times in a row.
It costs a_ij to change a single specific letter in the string from i to j. There are M different possible letters you can change each letter to.
Example: "abcde" is the input string. N = 5 (length of "abcde"), M = 5 (letters are A, B, C, D, E), and K = 2 (each letter must be repeated at least 2 times) Then we are given a M×M matrix of values a_ij, where a_ij is an integer in the range 0…1000 and a_ii = 0 for all i.
0 1 4 4 4
2 0 4 4 4
6 5 0 3 2
5 5 5 0 4
3 7 0 5 0
Here, it costs 0 to change from A to A, 1 to change from A to B, 4 to change from A to C, and so on. It costs 2 to change from B to A.
The optimal solution in this example is to change the a into b, change the d into e, and then change both e’s into c’s. This will take 1 + 4 + 0 + 0 = 5 moves, and the final combo string will be "bbccc".
It becomes complicated as it might take less time to switch from using button i to an intermediate button k and then from button k to button j rather than from i to j directly (or more generally, there may be a path of changes starting with i and ending with j that gives the best overall cost for switching from button i ultimately to button j).
To solve for this issue, I am treating the matrix as a graph, and then performing Floyd Warshall to find the fastest time to switch letters. This will take O(M^3) which is only 26^3.
My next step is to perform dynamic programming on each additional letter to find the answer. If someone could give me advice on how to do this, I would be thankful!
Here are some untested ideas. I'm not sure if this is efficient enough (or completely worked out) but it looks like 26 * 3 * 10^5. The recurrence could be converted to a table, although with higher Ks, memoisation might be more efficient because of reduced state possibilities.
Assume we've recorded 26 prefix arrays for conversion of the entire list to each of the characters using the best conversion schedule, using a path-finding method. This lets us calculate the cost of a conversion of a range in the string in O(1) time, using a function, cost.
A letter in the result can be one of three things: either it's the kth instance of character c, or it's before the kth, or it's after the kth. This leads to a general recurrence:
f(i, is_kth, c) ->
cost(i - k + 1, i, c) + A
where
A = min(
f(i - k, is_kth, c'),
f(i - k, is_after_kth, c')
) forall c'
A takes constant time since the alphabet is constant, assuming earlier calls to f have been tabled.
f(i, is_before_kth, c) ->
cost(i, i, c) + A
where
A = min(
f(i - 1, is_before_kth, c),
f(i - 1, is_kth, c'),
f(i - 1, is_after_kth, c')
) forall c'
Again A is constant time since the alphabet is constant.
f(i, is_after_kth, c) ->
cost(i, i, c) + A
where
A = min(
f(i - 1, is_after_kth, c),
f(i - 1, is_kth, c)
)
A is constant time in the latter. We would seek the best result of the recurrence applied to each character at the end of the string with either state is_kth or state is_after_kth.

Editing Excel Mod Formula

Could you please give me vba code that can solve this problem:
I want remainder in Mod Function can become equal to divisor.
Example: In normal situation Mod(132,12)=0 but I want when remainder is equal to divisor, last step of dividing that is dividing 12 on 12 doesn't do and remainder becomes 12.
Example
I wrote this code but it seems something is wrong. What's the problem?
Function XLMod(a, b)
XLMod = Int(a - (b * Int(a / b)))
If XLMod(a / 10, b) = 1 And XLMod(a, 10) = 2 Then
XLMod = b
End If
End Function
You need a special exception of the standard modulo function.
If the result of a normal division (a / b) would result in a number ending with 1 (e. g. 1, 31, 10001, 12341, ...), then you want it to return b.
Function XLMod(a, b)
XLMod = a Mod b
If XLMod = 0 And (a / b) Mod 10 = 1 Then XLMod = b
End Function

Something in for-loop breaks down after 17 loops

I have the following for-loop:
i = 1
y = 4
for column in new_columns:
df[column] = (df['column1'] * (1+G1)**i * (df['ER1'] - df['ER2']) \
* df['column2'] * (1+ df['Column3'])**i + df['column1'] \
* (1+G1)**i * df['E2'] * df['Column2'] * (1+ df['Column3'])**i \
* (1 - ER2) * df['Column4'])/ (1 + df['ER4'])**y
i += 1
y += 1
I noticed a bizarre kink in a graph made of the new columns and I decided to double-check the calculation by running the same thing in MS Excel. The ratio between the Python and Excel columns is 1 until loop number 17. On the 18th loop, the ratio jumps to 1.0249 (Python produces 2.5 % higher numbers) and stays there until the last loop (30). There is no kink on the graph produced in MS Excel. Any wise thoughts?
After spending about 8 hours on this, I finally noticed that I had a duplicate in new columnswhich obviously didn't show up on the resulting dataframe and hence the difference with Excel. Sorry guys.

Solving a large system of equations using VBA

I'm trying to solved a system of equations that results from estimating a 4th order differential equation. To do this requires creating a large matrix (A), usually 105 x 105, taking the inverse and multiplying by a 105 x 1 matrix (B). To do this, I'm using the linear algebra approach solving Ax = B.
Running the following code:
Dim A(1 To 105, 1 To 105) As Double
Dim B(1 To 105) As Double
Dim i As Integer
' Used to make sure all values of A are initialized to zero
For i = 1 To 105
For j = 1 To 105
A(i, j) = 0
Next
Next
For i = 1 To 105
A(i, i) = EI
A(i, i + 1) = -4 * EI + axial * h ^ 2
A(i, i + 2) = 6 * EI - 2 * axial * h ^ 2 + km(i) * h ^ 4
A(i, i + 3) = -4 * EI + axial * h ^ 2
A(i, i + 4) = EI
B(i) = W * h ^ 4
Next
Dim x(1 To 105) As Variant
x = Application.WorksheetFunction.MMult((Application.WorksheetFunction.MInverse(A)), B)
results in "Run-time error '1004': Unable to get the MInverse property of the WorksheetFunction class"
I've explored this error and it appears that it means I'm passing bad data to the function either text or blank values so I added the two loops at the top to initialize matrix A to 0 however this did nothing. After exploring some more I found some obscure post about a maximum size of matrices being 52 x 52 but wasn't able to find any more information about this.
Yes, there is a limit of 52 x 52, as mentioned in the official documentation.
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.worksheetfunction.minverse.aspx
EDIT
The documentation seems to be wrong. I tested =MINVERSE(A1:DA105) and it works well.
Very likely your matrix contains numbers that generate very large numbers and it fails. I populated my matrix 105 rows with numbers from 1 to 105 and it was failing. Then I put =RAND() in all the cells and it worked.
If the large numbers are the problem, perhaps there are tricks like populating the matrix with the logarithm of the numbers. It's a trick I use in other cases, I don't know if it can be used with a matrix.

using excel divide any value in a col by 1000, then multp new value by 10 & upd a running total cell, while leaving the orig value alone

For example if I enter a 2000 in B3, I would like that number divided by 1000, then multiplied by 10, and have the new value added to a running total. ie (2000/1000 * 10=20)
RunningTotal = 20
For clarity, if I enter 8000 in B4, then I would like to (8000/1000 * 10 = 80 )
RunningTotal = 100
Notice that
(x / 1000 * 10) + (y / 1000 * 10) = (x + y)/1000 * 10
So the equation for your running total cell only needs to be:
=SUM(B3:B10)/1000*10
Assuming B3:B10 is the appropriate range for your inputs.
equation formula
(x / 1000 * 10) + (y / 1000 * 10) = (x + y)/1000 * 10
=
(x+y) * 0.01
=
sum(B3:B?) * 0.01
You can simply record a macro:
http://office.microsoft.com/en-us/excel-help/create-a-macro-HP005204711.aspx
So you do everything you would like to calculate "by hand" while recording a macro and then you can call it at any time you want to execute.
Thanks for the replies.
I will be entering in values into col B(B3,B4...B200 etc.). The running total value will be displayed in it's own cell. The running total value will be updated upon save or enter. The orig value entered into the B column cells should remain the same.
The formula or calculation should occur anytime a value is entered into a cell within col B.
So if I have any number of values in col B, what I am expecting is:
1000/1000 * 10 = 10
5000/1000 * 10 = 60
8000/1000 * 10 = 140 etc.
If I use a separate cell to hold the runningTotal value, for ex K2...formula is: =C3/1000*10 ...how can I repeat this formula for subsequent cells in col B, but update the cell K2?
Must I use a holding column for each corresponding value entered in col B, then add the value to the K2 cell?
Ah, yes, I have managed to get it working by using a holding cell. if there is a more elegant approach, I am open to suggestions.
thanks for your advice.

Resources