SympifyError: SympifyError: index when using a loop - loops

I am having trouble using simpify when changing the parameters in a loop. Before adding the loop it worked just fine so I am a bit confused about what is going wrong. The idea is to calculate the fixed points for the above equations when having a varying parameter. I determined the parameters by using a random algorithm beforehand.
data used
index c1 c2 c3 c4 c5
2 0.182984 2.016811 0.655393 1.581344 1000.0
3 0.481093 3.696431 0.174021 2.604066 1000.0
4 2.651888 0.665661 2.010521 1.004902 1000.0
5 4.356905 3.805205 0.169469 0.188154 1000.0
6 0.618898 1.205760 0.394822 0.624573 1000.0
7 1.628458 0.908339 0.117855 0.801636 1000.0
8 1.084346 0.251490 5.008077 4.606338 1000.0
9 0.314420 4.553279 0.279103 1.136288 1000.0
10 0.309323 3.447195 0.769426 1.058890 1000.0
11 1.353905 5.034620 3.025668 0.136687 1000.0
12 0.294230 0.590507 0.203964 0.105073 1000.0
13 0.433693 1.040195 0.197015 0.214636 1000.0
14 5.597691 2.734779 0.298786 6.869852 1000.0
15 0.106748 0.329506 1.642285 2.259433 1000.0
16 7.065243 0.138986 6.280275 0.265305 1000.0
17 0.676381 0.263757 6.540224 2.890927 1000.0
18 0.646750 2.573060 0.157341 1.779078 1000.0
19 2.829030 0.208247 0.102454 0.117786 1000.0
20 3.973703 0.134666 1.099034 4.255214 1000.0
df1 = df[df.columns[1]]
df2 = df[df.columns[2]]
df3 = df[df.columns[3]]
df4 = df[df.columns[4]]
EQ=[]
for i in df[:5]:
a = df["c1"]
b = df["c2"]
c = df["c3"]
d = df["c4"]
Q = 1
a1 = 0
b1 = 0
c1 = 0
d1 = 0
u,v = sm.symbols('u,v', negative=False)
# equations
U = a * u -a1* v**2 - b*v+b1*v + Q
V = c * u -c1*u*v- d*v + d1 + Q
# use sympy's way of setting equations to zero
Uqual = sm.Eq(U, 0)
Vqual = sm.Eq(V, 0)
# compute fixed points
equilibria = sm.solve( (Uqual, Vqual), u,v)
print('The fixed point(s) of this system are: %s' %equilibria)
equilibria.append(equilibria)
SympifyError Traceback (most recent call last)
<ipython-input-81-7104e05ced6a> in <module>
16 V = c * u -c1*u*v- d*v + d1 + Q
17 # use sympy's way of setting equations to zero
---> 18 Uqual = sm.Eq(U, 0)
19 Vqual = sm.Eq(V, 0)
20
~\anaconda3\lib\site-packages\sympy\core\relational.py in __new__(cls, lhs, rhs, **options)
501 rhs = 0
502 evaluate = options.pop('evaluate', global_parameters.evaluate)
--> 503 lhs = _sympify(lhs)
504 rhs = _sympify(rhs)
505 if evaluate:
~\anaconda3\lib\site-packages\sympy\core\sympify.py in _sympify(a)
510
511 """
--> 512 return sympify(a, strict=True)
513
514
~\anaconda3\lib\site-packages\sympy\core\sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
431
432 if strict:
--> 433 raise SympifyError(a)
434
435 if iterable(a):
SympifyError: SympifyError: index
1 0.32539361355594*u - 0.153951771353544*v + 1
2 0.111286178007145*u - 0.211620881593914*v + 1
3 0.410704332996077*u - 0.338148622964363*v + 1
4 1.39126513227539*u - 0.715390758416011*v + 1
5 0.289981428632838*u - 3.76334113661812*v + 1
...
96 0.450838908230239*u - 7.00849756407416*v + 1
97 4.59646738213032*u - 1.45107766000711*v + 1
98 6.28779804684458*u - 0.395831415205476*v + 1
99 0.196464087698782*u - 0.205057919337616*v + 1
100 1.69031014508742*u - 0.140571509904066*v + 1
Length: 100, dtype: object

In an isympy session:
Make a sample dataframe:
In [11]: import pandas as pd
In [12]: df = pd.DataFrame(np.arange(12).reshape(3,4))
In [13]: df
Out[13]:
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
Set up a non-iterative case:
In [15]: u,v = symbols('u,v', negative=False)
In [16]: a,a1,b,b1 = 1,2,3,4
In [17]: U = a * u -a1* v**2 - b*v+b1*v
In [18]: U
Out[18]:
2
u - 2⋅v + v
Versus on with dataframe values:
In [19]: a,b = df[0],df[1]
In [20]: a,b
Out[20]:
(0 0
1 4
2 8
Name: 0, dtype: int64,
0 1
1 5
2 9
Name: 1, dtype: int64)
In [21]: U = a * u -a1* v**2 - b*v+b1*v
In [22]: U
Out[22]:
0 -2*v**2 + 3*v
1 4*u - 2*v**2 - v
2 8*u - 2*v**2 - 5*v
dtype: object
This U is a pandas Series, with object elements (which are sympy expressions). But U itself is not sympy.
Eq applied to the simple expression:
In [23]: Eq(Out[18],0)
Out[23]:
2
u - 2⋅v + v = 0
Your error - Eq applied to the Series:
In [24]: Eq(Out[22],0)
---------------------------------------------------------------------------
SympifyError Traceback (most recent call last)
Input In [24], in <cell line: 1>()
----> 1 Eq(Out[22],0)
File /usr/local/lib/python3.8/dist-packages/sympy/core/relational.py:626, in Equality.__new__(cls, lhs, rhs, **options)
624 rhs = 0
625 evaluate = options.pop('evaluate', global_parameters.evaluate)
--> 626 lhs = _sympify(lhs)
627 rhs = _sympify(rhs)
628 if evaluate:
File /usr/local/lib/python3.8/dist-packages/sympy/core/sympify.py:528, in _sympify(a)
502 def _sympify(a):
503 """
504 Short version of :func:`~.sympify` for internal usage for ``__add__`` and
505 ``__eq__`` methods where it is ok to allow some things (like Python
(...)
526
527 """
--> 528 return sympify(a, strict=True)
File /usr/local/lib/python3.8/dist-packages/sympy/core/sympify.py:449, in sympify(a, locals, convert_xor, strict, rational, evaluate)
446 continue
448 if strict:
--> 449 raise SympifyError(a)
451 if iterable(a):
452 try:
SympifyError: SympifyError: 0 -2*v**2 + 3*v
1 4*u - 2*v**2 - v
2 8*u - 2*v**2 - 5*v
dtype: object
Eq() does not have a 'iterate over Series' (or even over list) capability.
We can iterate (list comprehension) and apply the Eq to each terms of the Series:
In [25]: [Eq(U[i],0) for i in range(3)]
Out[25]:
⎡ 2 2 2 ⎤
⎣- 2⋅v + 3⋅v = 0, 4⋅u - 2⋅v - v = 0, 8⋅u - 2⋅v - 5⋅v = 0⎦
As a general rule, sympy and pandas/numpy does not work well.

It's hard to understand what you are trying to achieve with the code you posted above. So, the following represents my guess:
# NOTE: the following variables are of type
# pandas.core.series.Series. They are iterables
# (think of them as arrays)
a = df["c1"]
b = df["c2"]
c = df["c3"]
d = df["c4"]
# constants
Q = 1
a1 = 0
b1 = 0
c1 = 0
d1 = 0
# symbols
u, v = symbols('u, v', negative=False)
# equations
# NOTE: because a, b, c, d are iterables, then U, V
# will be iterables too. Each element will be a SymPy
# expression because you used the symbols u and v.
U = a * u - a1 * v**2 - b * v + b1 * v + Q
V = c * u - c1 * u * v - d * v + d1 + Q
EQ = []
# loop over the equations and solve them
for u_eq, v_eq in zip(U, V):
# here we are asking to solve u_eq=0 and v_eq=0
equilibria = solve((u_eq, v_eq), (u, v))
EQ.append(equilibria)
print('The fixed point(s) of this system are: %s' % equilibria)

Related

Element wise comparison in R

I'm attempting to write a for loop that will compare values between two individuals, but not the same individual. The following data frame contains values for five subjects:
Value1
Subject1 0
Subject2 1
Subject3 5
Subject4 6
Subject5 8
I've written a double loop that creates a 'Value2' variable based on the following criteria:
If the subject has a larger Value1, then the result is +1.
If the subject has an equal Value1, then the result is 0.
If the subject has a smaller Value1, then the result is -1.
For example, Subject 1's Value1 is smaller than the other four subjects; this should result in -4. So far the loop I've written works for the first subject but fails to iterate to the second subject.
Value2<-0
i = 0
w = 0
for(i in 1:length(Value1)){
for(j in 1:length(Value1)){
if(i != j){
Value1[i] = w
if(w > Value1[j]){
Value2[i] = Value2[i] + 1
}
if(w < Value1[j]){
Value2[i] = Value2[i] - 1
}
if(w == Value1[j]){
Value2[i] = Value2[i] + 0
}
}
}
}
If I'm understanding the problem correctly, this should give you what you want
x <- c(0, 1, 5, 6, 8)
colSums(outer(x, x, '<')) - colSums(outer(x, x, '>'))
# [1] -4 -2 0 2 4
Or
-colSums(sign(outer(x, x, '-')))
# [1] -4 -2 0 2 4
Edit: If your vector is large (or even if it isn't, really) use d.b.'s rank method instead. The outer function will create an NxN matrix where N is the length of x. For example, when x is sample(1e5) outer will attempt to create a matrix >30Gb in size! This means most people's laptops in 2019 don't even have enough memory for this method to work on large vectors. With this same x, the method using rank provided by d.b. returns the result almost instantly.
Benchmark for vector of size 1000
x <- sample(1000)
microbenchmark(
outer_diff = colSums(-sign(outer(x, x, '-'))),
outer_gtlt = colSums(outer(x, x, '<')) - colSums(outer(x, x, '>')),
rank = {r <- rank(x); 2*(r - mean(r))}
)
# Unit: microseconds
# expr min lq mean median uq max neval cld
# outer_diff 15930.26 16872.4175 20946.2980 18030.776 25346.677 38668.324 100 b
# outer_gtlt 14168.21 15120.4165 28970.7731 16698.264 23857.651 352390.298 100 b
# rank 111.18 141.5385 170.8885 177.026 188.513 282.257 100 a
x = c(0, 1, 5, 6, 8)
r = rank(x)
ans = 2 * (r - mean(r))
ans
#[1] -4 -2 0 2 4
#IceCreamToucan's benchmark considers cases with distinct values (sampling without replacement), but if we extend to repeated values (covered by criterion 2 in the OP), I figured tabulating first saves time.
library(data.table)
# from #d.b's answer and comments from d.b, ICT
fdb = function(x) {
r = frank(x)
2 * (r - mean(r))
}
# from #chinsoon's comment and some algebra
fdb2 = function(x) {
r = frank(x)
2 * r - length(x) - 1
}
# tabulation with data.table
ff = function(x){
nx = length(x)
xDT = setDT(list(x=x))
resDT = xDT[, .N, keyby=x][, res := 2L*cumsum(N) - N - nx]
resDT[xDT, x.res]
}
Sample data and results:
nv = 1e4 # number of values
n = 1e7 # length of vector
x = sample(nv, n, replace=TRUE)
system.time(res_fdb <- fdb(x))
# user system elapsed
# 0.32 0.09 0.24
system.time(res_fdb2 <- fdb2(x))
# user system elapsed
# 0.25 0.13 0.27
system.time(res_ff <- ff(x))
# user system elapsed
# 0.58 0.24 0.50
identical(res_ff, as.integer(res_fdb)) # TRUE
identical(res_ff, as.integer(res_fdb2)) # TRUE
Turns out ff() not as fast as direct use of data.table::frank, taking roughly twice as long because grouping by distinct values is done twice: once to count, and again in a lookup.
I guess the tabulation can also be done with base R's table.
ft = function(x){
nx = length(x)
N = table(x)
cN = cumsum(N)
res = 2L*cN - N - nx
as.vector(res[as.character(x)])
}
system.time(res_ft <- ft(x))
# user system elapsed
# 7.58 0.34 7.93
identical(res_ff, res_ft)
# [1] TRUE

error: sub2ind: all subscripts must be of the same size using Octave 4.2.2

I'm getting an error: sub2ind: all subscripts must be of the same size. The line that seems to be the issue is idx = sub2ind ([n m], r, c)
If I try and make the array certain sizes I get that error (see some of the values below that cause the issue)
%values that cause the error
array_rows=3; %3,3,7,7
array_cols=6; %6,5,3,5
How can I get it to work with these values and others that cause this error (I'm sure there are more values that cause this error)?
The Original question: was answered and solved by Andy here
See code below:
function r = rndtrip (n, m, v)
rv = #(x) x - 2 * (v - 1);
r = [v * ones(1,rv(m)-1) v:n-v+1 (n-v+1)*ones(1,rv(m)-2)];
if (rv(m) > 1)
r = [r n-v+1:-1:v+1];
endif
endfunction
function idx = ring (n, m , v)
if (2*(v-1) > min (n, m))
r = [];
else
r = rndtrip (n, m, v);
c = circshift (rndtrip (m, n, v)(:), - n + 2 * v - 1).';
idx = sub2ind ([n m], r, c)
endif
endfunction
# your array
array_rows=3; %3,3,7,7
array_cols=6; %6,5,3,5
I_orig = reshape (1:array_rows*array_cols, array_cols, array_rows).' %reshape array %I_orig = reshape (1:30, 5, 6).'
[rw_orig col_orig]=size(I_orig);
I_new=I_orig; %where the new rotated values will be stored
min_rows_cols=min(rw_orig,col_orig); %get min number of row - columns used to calc number of rings
r=(-1).^(1:ceil(min_rows_cols/2)) % (toggles beween -1 1)
%r = [1 -1] %used to have ring rotate CCW or CW can be done manually
for k = 1:numel(r)
idx = ring (rows(I_new), columns(I_new), k);
I_new(idx) = I_new(circshift(idx(:), r(k)));
endfor
I_new
I_orig =
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
r =
-1 1
idx =
1 4 7 10 13 16 17 18 15 12 9 6 3 2
error: sub2ind: all subscripts must be of the same size
error: called from ring at line 16 column 9
Values that Work with the Code run on TIO
Values that don't work with the Code run on TIO
Ps: I'm using Ubuntu 18.04 Octave 4.2.2

Vectorize 2d convolution on matlab

I got this Code for computing two dimensional convolution for two given arrays.
[r,c] = size(x);
[m,n] = size(y);
h = rot90(y, 2);
center = floor((size(h)+1)/2);
Rep = zeros(r + m*2-2, c + n*2-2);
return
for x1 = m : m+r-1
for y1 = n : n+r-1
Rep(x1,y1) = x(x1-m+1, y1-n+1);
end
end
B = zeros(r+m-1,n+c-1);
for x1 = 1 : r+m-1
for y1 = 1 : n+c-1
for i = 1 : m
for j = 1 : n
B(x1, y1) = B(x1, y1) + (Rep(x1+i-1, y1+j-1) * h(i, j));
end
end
end
end
How can i vectorize it , so no for loops exist ?
Thanks in advance.
Here's what I came up with:
%// generate test matrices
x = randi(12, 4, 5)
y = [2 2 2;
2 0 2;
2 2 2]
[r,c] = size(x);
%[m,n] = size(y); %// didn't use this
h = rot90(y, 2);
center = floor((size(h)+1)/2);
Rep = zeros(size(x)+size(h)-1); %// create image of zeros big enough to pad x
Rep(center(1):center(1)+r-1, center(2):center(2)+c-1) = x; %// and copy x into the middle
%// all of this can be compressed onto one line, if desired
%// I'm just breaking it out into steps for clarity
CRep = im2col(Rep, size(h), 'sliding'); %// 'sliding' is the default, but just to be explicit
k = h(:); %// turn h into a column vector
BRow = bsxfun(#times, CRep, k); %// multiply k times each column of CRep
B = reshape(sum(BRow), r, c) %// take the sum of each column and reshape to match x
T = conv2(Rep, h, 'valid') %// take the convolution using conv2 to check
assert(isequal(B, T), 'Result did not match conv2.');
Here are the results of a sample run:
x =
11 12 11 2 8
5 9 2 3 2
7 9 3 4 8
7 10 8 5 4
y =
2 2 2
2 0 2
2 2 2
B =
52 76 56 52 14
96 120 106 80 50
80 102 100 70 36
52 68 62 54 34
T =
52 76 56 52 14
96 120 106 80 50
80 102 100 70 36
52 68 62 54 34

Karatsuba Algorithm: splitting strings

I am trying to implement the Karatsuba algorithm in C.
I work with char strings (which are digits in a certain base), and although I think I have understood most of the Karatsuba algorithm, I do not get where I should split the strings to multiply.
For example, where should I cut 123 * 123, and where should I cut 123 * 12?
I can't get to a solution that works with both these calculations.
I tried to cut it in half and flooring the result when the number if odd, but it did not work, and ceiling does not work too.
Any clue?
Let a, b, c, and d be the parts of the strings.
Let's try with 123 * 12
First try (a = 1, b = 23, c = 1, d = 2) (fail)
z0 = a * c = 1
z1 = b * d = 46
z2 = (a + b) * (c + d) - z0 - z1 = 24 * 3 - 1 - 46 = 72 - 1 - 46 = 25
z0_padded = 100
z2_padded = 250
z0_padded + z1 + z2_padded = 100 + 46 + 250 = 396 != 123 * 12
Second try (a = 12, b = 3, c = 12, d = 0) (fail)
z0 = 144
z1 = 0
z2 = 15 * 12 - z1 - z0 = 180 - 144 = 36
z0_padded = 14400
z2_padded = 360
z0_padded + z1 + z2_padded = 14760 != 1476
Third try (a = 12, b = 3, c = 0, d = 12) (success)
z0 = 0
z1 = 36
z2 = 15 * 12 - z0 - z1 = 144
z0_padded = 0
z2_padded = 1440
z0_padded + z1 + z2_padded = 1476 == 1476
Let's try with 123 * 123
First try (a = 1, b = 23, c = 1, d = 23) (fail)
z0 = 1
z1 = 23 * 23 = 529
z2 = 24 * 24 - z0 - z1 = 46
z0_padded = 100
z2_padded = 460
z0_padded + z1 + z2_padded = 561 != 15129
Second try (a = 12, b = 3, c = 12, d = 3) (success)
z0 = 12 * 12 = 144
z1 = 3 * 3 = 9
z2 = 15 * 15 - z0 - z1 = 72
z0_padded = 14400
z2_padded = 720
z0_padded + z1 + z2_padded = 15129 == 15129
Third try (a = 12, b = 3, c = 1, d = 23) (fail)
z0 = 12
z1 = 3 * 23 = 69
z2 = 15 * 24 - z0 - z1 = 279
z0_padded = 1200
z2_padded = 2799
z0_padded + z1 = z2_padded = 4068 != 15129
Here, I do not get where I messed this up. Note that my padding method adds n zeroes at the end of a number where n = m * 2 and m equals the size of the longest string divided by two.
EDIT
Now that I have understood that b and d must be of the same length, it works almost everytime, but there are still exceptions: for example 1234*12
a = 123
b = 4
c = 1
d = 2
z0 = 123
z1 = 8
z2 = 127 * 3 - 123 - 8 = 250
z0_padded = 1230000
z2_padded = 25000
z0_padded + z1 + z2_padded = 1255008 != 14808
Here, assuming I split the strings correctly, the problem is the padding, but I do not get how I should pad. I read on Wikipedia that I should pad depending on the size of the biggest string (see a few lines up), there should be another solution.
The Karatsuba algorithm is a nice way to perform multiplications.
If you want it to work, b and d must be of the same length.
Here are two possibilities to compute 123x12 :
a= 1;b=23;c=0;d=12;
a=12;b= 3;c=1;d= 2;
Let's explain how it works for the second case :
123=12×10+3
12= 1×10+2
123×12=(12×10+3)×(1×10+2)
123×12=12×1×100+ (12×2+3×1)×10+3×2
123×12=12×1×100+((12+3)×(1+2)-12×1-3×2)×10+3×2
Let's explain how it works for the first case :
123=1×100+23
12=0×100+12
123×12=(1×100+23)×(0×100+12)
123×12=1×0×10000+ (1×12+23×0)×100+23×12
123×12=1×0×10000+((1+23)×(0+12)-1×0-23×12)×100+23×12
It also works with 10^k, 2^k or n instead of 10 or 100.

Matlab - arranging numbers

I have vectors m, x, y & I want m1, x1, y1 as commented below:
% given
m = [-4 -3 -2 2 3 4];
x = [2 5 6 7 9 1];
y = [10 23 34 54 27 32];
% required
% m1 = [2 3 4]; % only +ve value from m
% x1 = [13 14 3]; % adding numbers(in x) corres. to -ve & +ve value in m & putting below 2, 3, 4 respectively
% y1 = [88 50 42]; % adding numbers(in y) corres. to -ve & +ve value in m & putting below 2, 3, 4 respectively
m1 = m(m > 0) % this gives me m1 as required
Any hint for x1, y1 will be very helpful.
Assuming m is built as [vectorNegativeReversed, vectorPositiveOriginal] the solution can be quite straightforward:
p = numel(m)/2;
m1 = m(p+1:end)
x1 = x(p+1:end) + x(p:-1:1)
y1 = y(p+1:end) + y(p:-1:1)
What about some flippy action:
m = [-4 -3 -2 2 3 4];
x = [2 5 6 7 9 1];
y = [10 23 34 54 27 32];
idx = find( (m > 0) );
xdi = find( ~(m > 0) );
m1 = m(idx)
x1 = fliplr( x(xdi) ) + x(idx)
y1 = fliplr( y(xdi) ) + y(idx)
returning:
m1 =
2 3 4
x1 =
13 14 3
y1 =
88 50 42

Resources