I work in the Gaussian integers and use factor on them with success. However, occasionally the imaginary part can vanish, but I still want a factorization in the Gaussian integers.
As an example, the program for(j=-3, 3, print(j,": ",factor(17+j*I))) writes:
-3: [-I, 1; 1 + I, 1; 10 + 7*I, 1]
-2: [-I, 1; 2 + 17*I, 1]
-1: [-1, 1; 1 + I, 1; 1 + 2*I, 1; 2 + 5*I, 1]
0: Mat([17, 1])
1: [-I, 1; 1 + I, 1; 2 + I, 1; 5 + 2*I, 1]
2: Mat([17 + 2*I, 1])
3: [-I, 1; 1 + I, 1; 7 + 10*I, 1]
I would like the middle line in the output to be different:
-3: [-I, 1; 1 + I, 1; 10 + 7*I, 1]
-2: [-I, 1; 2 + 17*I, 1]
-1: [-1, 1; 1 + I, 1; 1 + 2*I, 1; 2 + 5*I, 1]
0: [-I, 1; 4 + I, 1; 1 + 4*I, 1]
1: [-I, 1; 1 + I, 1; 2 + I, 1; 5 + 2*I, 1]
2: Mat([17 + 2*I, 1])
3: [-I, 1; 1 + I, 1; 7 + 10*I, 1]
Is there some setting to do that? Or will I have to write my own "wrapper" that treats this case specially?
Function factor(x, {D}) receives an optional parameter D indicating the factorisation domain. So just specify the target domain in call factor(x, I):
> for(j=-3, 3, print(j,": ",factor(17+j*I, I)))
-3: [-I, 1; 1 + I, 1; 10 + 7*I, 1]
-2: [-I, 1; 2 + 17*I, 1]
-1: [-1, 1; 1 + I, 1; 1 + 2*I, 1; 2 + 5*I, 1]
0: [-I, 1; 4 + I, 1; 1 + 4*I, 1]
1: [-I, 1; 1 + I, 1; 2 + I, 1; 5 + 2*I, 1]
2: Mat([17 + 2*I, 1])
3: [-I, 1; 1 + I, 1; 7 + 10*I, 1]
Related
How to loop through a dataframe series multiple times using a recursive function?
I am trying to get a simple case to work and use it in a more complicated function.
I am using a simple dataframe:
df = pd.DataFrame({'numbers': [1,2,3,4,5]
I want to iterate through the rows multiple time and sum the values. Each iteration, the index starting point increments by 1.
def recursive_sum(df, mysum=0, count=0):
df = df.iloc[count:]
if len(df.index) < 2:
return mysum
else:
for i in range(len(df.index)):
mysum += df.iloc[i, 0]
count += 1
return recursive_sum(df, mysum, count)
I think I should get:
#Iteration 1: count = 0, len(df.index) = 5 < 2, mysum = 1 + 2 + 3 + 4 + 5 = 15
#Iteration 2: count = 1, len(df.index) = 4 < 2, mysum = 15 + 2 + 3 + 4 + 5 = 29
#Iteration 3: count = 2, len(df.index) = 3 < 2, mysum = 29 + 3 + 4 + 5 = 41
#Iteration 4: count = 2, len(df.index) = 2 < 2, mysum = 41 + 4 + 5 = 50
#Iteration 5: count = 2, len(df.index) = 1 < 2, mysum = 50
But I am returning 38.
Just fixed it:
def recursive_sum(df, mysum=0, count=0):
if(len(df.index) - count) < 2:
return mysum
else:
for i in range(count, len(df.index)):
mysum += df.iloc[0]
count += 1
return recursive_sum(df, mysum, count)
I am novice in analysing time complexity.some one can help me with the time complexity of below algorithm?
public void test(int n)
{
for(int i=1;i<=n;i=i+2)
{
for(int j=1;j<=i;j++)
{}
}
}
outer loop will run n/2 times.Inner loop will run (1+3+5+7+9...n) times.
what will be time complexity of inner loop and how can we calculate sum of such arithmitic progression?
Assume n is odd. Then n = 2k + 1 for some k. Now
1 + 3 + … + n
= 1 + 3 + … + 2k+1
= (1 + 3 + … + 2k + 1) + (1 + 1 + … + 1) - (1 + 1 + … + 1)
= (1 + 1) + (3 + 1) + … + (2k + 1 + 1) - (1 + 1 + … + 1)
= 2 + 4 + … + 2k+2 - (1 + 1 + … + 1)
= 2(1 + 2 + … + k+1) - (1 + 1 + … + 1)
= 2(k+1)(k+2)/2 - (k+1)
= k^2 + 3k + 2 - k - 1
= k^2 + 2k + 1
= (k+1)^2
= (n+1)^2/4
We can test a few terms...
n f(n) Series Sum
1 1 1 = 1
3 4 1 + 3 = 4
5 9 1 + 3 + 5 = 9
7 16 1 + 4 + 5 + 7 = 16
Looks like it checks out.
I want to sum over a range (Areas) except if j is not equal to k.
Can anybody help me?
I tried:
forall( k in Areas )
sum ( j in Areas: j!=k ) X[k][j] == 1;
Also tried:
forall( k in Areas )
sum ( j in Areas) (j!=k)*X[k][j] == 1;
int NbAreas = 5;
range Areas = 1..NbAreas;
float P[Areas] = [0, 0.3, 0.65, 0.2, 0.1];
float D[Areas] = [0, 7, 5, 3, 9];
float FROMTO[Areas][Areas] = [
[0, 2, 5, 1, 3],
[2, 0, 4, 3, 8],
[5, 4, 0, 6, 2],
[1, 3, 6, 0, 7],
[3, 8, 2, 7, 0]];
dvar int Y[Areas];
dvar int T[Areas];
dvar int X[Areas][Areas] in 0..1;
maximize sum( i in Areas ) P[i] * Y[i];
subject to {
forall( k in Areas )
sum ( j in Areas: j!=k) X[k][j] == 1;
forall( k in Areas)
sum ( i in Areas: i!=k) X[i][k] == 1;
forall( i in Areas) forall (j in Areas) T[i] + FROMTO[i][j] - T[j] - 100*(1-X[i][j]) <= 0;
T[1] == 0;
forall( i in Areas: i!=1) T[i] - D[i] - 1000*(1-Y[i]) <= 0;
}
I take it you meant
I want to sum over a range (Areas) except if j is equal (instead
of "not equal") to k.
I also assume that your issue is that the model you posted is infeasible. You should label your constraints so that the conflict refiner can run and then look at the results of the conflict refiner. If I label your constraints like so:
maximize sum( i in Areas ) P[i] * Y[i];
subject to {
forall( k in Areas )
sum1: sum ( j in Areas: j!=k) X[k][j] == 1;
forall( k in Areas)
sum2: sum ( i in Areas: i!=k) X[i][k] == 1;
forall( i in Areas) forall (j in Areas)
fromto: T[i] + FROMTO[i][j] - T[j] - 100*(1-X[i][j]) <= 0;
T[1] == 0;
forall( i in Areas: i!=1)
limit: T[i] - D[i] - 1000*(1-Y[i]) <= 0;
Then I get this conflict:
sum1(1): X(1)(2) + X(1)(3) + X(1)(4) + X(1)(5) = 1
sum1(2): X(2)(1) + X(2)(3) + X(2)(4) + X(2)(5) = 1
sum1(3): X(3)(1) + X(3)(2) + X(3)(4) + X(3)(5) = 1
sum1(4): X(4)(1) + X(4)(2) + X(4)(3) + X(4)(5) = 1
sum1(5): X(5)(1) + X(5)(2) + X(5)(3) + X(5)(4) = 1
fromto(1)(2): 100 X(1)(2) + T(1) - T(2) <= 98
fromto(1)(3): 100 X(1)(3) + T(1) - T(3) <= 95
fromto(1)(4): 100 X(1)(4) + T(1) - T(4) <= 99
fromto(1)(5): 100 X(1)(5) + T(1) - T(5) <= 97
fromto(2)(1): 100 X(2)(1) - T(1) + T(2) <= 98
fromto(2)(3): 100 X(2)(3) + T(2) - T(3) <= 96
fromto(2)(4): 100 X(2)(4) + T(2) - T(4) <= 97
fromto(2)(5): 100 X(2)(5) + T(2) - T(5) <= 92
fromto(3)(1): 100 X(3)(1) - T(1) + T(3) <= 95
fromto(3)(2): 100 X(3)(2) - T(2) + T(3) <= 96
fromto(3)(4): 100 X(3)(4) + T(3) - T(4) <= 94
fromto(3)(5): 100 X(3)(5) + T(3) - T(5) <= 98
fromto(4)(1): 100 X(4)(1) - T(1) + T(4) <= 99
fromto(4)(2): 100 X(4)(2) - T(2) + T(4) <= 97
fromto(4)(3): 100 X(4)(3) - T(3) + T(4) <= 94
fromto(4)(5): 100 X(4)(5) + T(4) - T(5) <= 93
fromto(5)(1): 100 X(5)(1) - T(1) + T(5) <= 97
fromto(5)(2): 100 X(5)(2) - T(2) + T(5) <= 92
fromto(5)(3): 100 X(5)(3) - T(3) + T(5) <= 98
fromto(5)(4): 100 X(5)(4) - T(4) + T(5) <= 93
So it seems your data in FROMTO and T does not allow a feasible solution.
You wrote "except if j is not equal to k".
So instead of
forall( k in Areas)
sum ( i in Areas: i!=k) X[i][k] == 1;
I would write
forall( k in Areas)
sum ( i in Areas: i==k) X[i][k] == 1;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this function in vb which I want to write in matlab. Does anyone know how this could be written using for loops instead of the goto keyword?
FOR i = 1 TO 101
X(i) = (i - 1) * .5 / 50:
X(i) = X(i) / 2 / PI / F:
NEXT i
j = 6
870 FOR i = 1 TO 100
IF X(i) <= j * .005 AND X(i + 1) >= j * .005 THEN GOTO 890 ELSE GOTO 1000
890 AZ4(j) = AY4(i) + (AY4(i + 1) - AY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
BZ4(j) = BY4(i) + (BY4(i + 1) - BY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
CZ4(j) = CY4(i) + (CY4(i + 1) - CY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
DZ4(j) = DY4(i) + (DY4(i + 1) - DY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
EZ4(j) = EY4(i) + (EY4(i + 1) - EY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
AZT(j) = AYT(i) + (AYT(i + 1) - AYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
BZT(j) = BYT(i) + (BYT(i + 1) - BYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
CZT(j) = CYT(i) + (CYT(i + 1) - CYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
DZT(j) = DYT(i) + (DYT(i + 1) - DYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
EZT(j) = EYT(i) + (EYT(i + 1) - EYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
i = 101
1000 NEXT i
j = j + 1
IF j * .005 > X(101) THEN GOTO 1040
GOTO 870
1040 FOR i = 1 TO 126
AY4(i) = AZ4(i): BY4(i) = BZ4(i): CY4(i) = CZ4(i): DY4(i) = DZ4(i): EY4(i) = EZ4(i)
AYT(i) = AZT(i): BYT(i) = BZT(i): CYT(i) = CZT(i): DYT(i) = DZT(i): EYT(i) = EZT(i)
NEXT i
FOR i = 1 TO 126
X(i) = i * .005
NEXT i
I will write just the skeleton of the code, the rest of the statements must be completed by you. I will use ii and jj instead of i and j because these have special meaning in MATLAB (complex square root of -1).
for ii = 1:101
% statements
end;
jj = 6;
while true
for ii = 1:100
if (cond1) && (cond2) % replace with actual conditions
% statements from label 890
break;
end;
end;
ii = 101; % useless, but...
jj = jj+1;
if cond3 % replace with actual condition
break;
end;
end;
for ii = 1:126
% statements
end;
for ii = 1:126
% statements
end;
I've been working on Project Euler questions as part of learning how to code in Lasso and am wondering if my solution can be improved upon. Here is what I've got below for question #1 in Lasso 8 code, and it returns the correct answer:
var ('total' = 0);
loop(1000-1);
loop_count % 3 == 0 || loop_count % 5 == 0 ? $total += loop_count;
/loop;
output($total);
My question: is there a better or faster way to code this? Thanks!
Actually Chris it looks like my L9 code answer was almost exactly the same. However what I had to do to time is was wrap it in a loop to time it 1000 times.
Lasso 9 can do Microseconds, whereas versions prior can only time in milliseconds.
Below are 3 ways - the first is yours, then my two versions.
define br => '<br>'
local(start_time = micros)
loop(1000)=>{
var ('total' = 0);
loop(1000-1);
loop_count % 3 == 0 || loop_count % 5 == 0 ? $total += loop_count;
/loop;
$total;
}
'Avg (L8 code in 9): '+(micros - #start_time)/1000+' micros'
br
br
local(start_time = micros)
loop(1000)=>{
local(sum = 0)
loop(999)=>{ loop_count % 3 == 0 || loop_count % 5 == 0 ? #sum += loop_count }
#sum
}
'Avg (incremental improvement): '+(micros - #start_time)/1000+' micros'
br
br
local(start_time = micros)
loop(1000)=>{
local(sum = 0)
loop(999)=>{ not (loop_count % 3) || not(loop_count % 5) ? #sum += loop_count }
#sum
}
'Avg using boolean not: '+(micros - #start_time)/1000+' micros'
The output is:
Avg (L8 code in 9): 637 micros
Avg (incremental improvement): 595 micros
Avg using boolean not: 596 micros
Note that I didn't use "output" as it's redundant in many situations in 8 and completely redundant 9 :)
There is a fun story about how Gauss once summed numbers, which involves a strategy which can help to avoid the loop.
local('p' = 3);
local('q' = 5);
local('n' = 1000);
local('x' = integer);
local('before');
local('after');
#before = micros
loop(1000) => {
/* In the tradition of Gauss */
local('n2' = #n - 1)
local('pq' = #p * #q)
local('p2' = #n2 / #p)
local('q2' = #n2 / #q)
local('pq2' = #n2 / #pq)
local('p3' = (#p2 + 1) * (#p2 / 2) + (#p2 % 2 ? #p2 / 2 + 1 | 0))
local('q3' = (#q2 + 1) * (#q2 / 2) + (#q2 % 2 ? #q2 / 2 + 1 | 0))
local('pq3' = (#pq2 + 1) * (#pq2 / 2) + (#pq2 % 2 ? #pq2 / 2 + 1 | 0))
#x = #p * #p3 + #q * #q3 - #pq * #pq3
}
#after = micros
'Answer: ' + #x + '<br/>\n'
'Average time: ' + ((#after - #before) / 1000) + '<br/>\n'
/* Different numbers */
#p = 7
#q = 11
#before = micros
loop(1000) => {
/* In the tradition of Gauss */
local('n2' = #n - 1)
local('pq' = #p * #q)
local('p2' = #n2 / #p)
local('q2' = #n2 / #q)
local('pq2' = #n2 / #pq)
local('p3' = (#p2 + 1) * (#p2 / 2) + (#p2 % 2 ? #p2 / 2 + 1 | 0))
local('q3' = (#q2 + 1) * (#q2 / 2) + (#q2 % 2 ? #q2 / 2 + 1 | 0))
local('pq3' = (#pq2 + 1) * (#pq2 / 2) + (#pq2 % 2 ? #pq2 / 2 + 1 | 0))
#x = #p * #p3 + #q * #q3 - #pq * #pq3
}
#after = micros
'Answer: ' + #x + '<br/>\n'
'Average time: ' + ((#after - #before) / 1000) + '<br/>\n'
The output is:
Answer: 233168<br/>
Average time: 3<br/>
Answer: 110110<br/>
Average time: 2<br/>
Although the first time I ran it, that first average time was 18 instead of 3. Maybe Lasso is doing something smart for subsequent runs, or maybe it was just bad luck.
n = input number
x = (n-1)/3 = count of 3 divisible numbers.*
sum3 = (3*x*(x+1)) / 2 = sum of those numbers.**
y = (n-1)/5 = count of 5 divisible numbers.
sum5 = (5*y*(y+1)) / 2 = sum of those numbers.
half_Ans = sum3 + sum5
but 15, 30, 45... count twice (in both sum3 & sum5).
so remove it one time, so only they count once.
z = (n-1)/15 = count of 15 divisible numbers.
sum15 = (15*z*(z+1)) / 2 = sum of those numbers.
Answer = half_Ans - sum15
* => (n-1)/3 gives count of 3 divisible numbers.
if n = 100 we need to count of (3, 6, 9, ..., 99)
3 is 1st, 6 is 2nd, .... so on 99 is 33rd
so total count of those number is gain by last number / 3
last number is near to our input n (specifically less than input n)
if n = 99 we must not count 99, because statement is "find the sum of all the multiples of 3 or 5 below n".
so w/o subtract 1 last unwanted number also count, if n is divisible by 3.
** => (3*x*(x+1)) / 2 gives sum of those numbers
if n = 100 sum id 3 + 6 + 9 + ... + 99
all component are multiple of 3.
so 3 + 6 + 9 + ... + 99 = 3*(1 + 2 + 3 + ... + 33)
sum of 1 to m is (m*(m+1)) / 2
so 3 + 6 + 9 + ... + 99 = (3*33*(33+1)) / 2
here m for 1 to m is last number or total number of that sequence
or length of sequence that's why we find count of divisible numbers.