Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using python-style for loops in calculus #36087

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sage/calculus/interpolation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ cdef class Spline:
if i < len(self.v):
self.v[i] = xy
else:
for j from len(self.v) <= j <= i:
for j in range(len(self.v), i + 1):
self.v.append((0, 0))
self.v[i] = xy
self.stop_interp()
Expand Down Expand Up @@ -262,7 +262,7 @@ cdef class Spline:
raise MemoryError

cdef int i
for i from 0 <= i < n:
for i in range(n):
self.x[i] = v[i][0]
self.y[i] = v[i][1]

Expand Down
54 changes: 27 additions & 27 deletions src/sage/calculus/ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ cdef int c_jac(double t,double *y,double *dfdy,double *dfdt,void *params):
cdef int param_n
cdef PyFunctionWrapper wrapper
wrapper = <PyFunctionWrapper > params
y_n=wrapper.y_n
y_list=[]
for i from 0<=i<y_n:
y_n = wrapper.y_n
y_list = []
for i in range(y_n):
y_list.append(y[i])
try:
if len(wrapper.the_parameters)==0:
jac_list=wrapper.the_jacobian(t,y_list)
else:
jac_list=wrapper.the_jacobian(t,y_list,wrapper.the_parameters)
for i from 0<=i<y_n:
for j from 0<=j<y_n:
for i in range(y_n):
for j in range(y_n):
dfdy[i*y_n+j]=jac_list[i][j]

for i from 0 <=i<y_n:
for i in range(y_n):
dfdt[i]=jac_list[y_n][i]

return GSL_SUCCESS
Expand All @@ -91,17 +91,17 @@ cdef int c_f(double t,double* y, double* dydt,void *params):

cdef PyFunctionWrapper wrapper
wrapper = <PyFunctionWrapper> params
y_n= wrapper.y_n
y_list=[]
for i from 0<=i<y_n:
y_n = wrapper.y_n
y_list = []
for i in range(y_n):
y_list.append(y[i])
try:
if len(wrapper.the_parameters)!=0:
dydt_list=wrapper.the_function(t,y_list,wrapper.the_parameters)
else:
dydt_list=wrapper.the_function(t,y_list)
for i from 0<=i<y_n:
dydt[i]=dydt_list[i]
for i in range(y_n):
dydt[i] = dydt_list[i]
return GSL_SUCCESS
except Exception:
return -1
Expand Down Expand Up @@ -453,15 +453,15 @@ class ode_solver():
cdef double * scale_abs_array
scale_abs_array=NULL

y= <double*> sig_malloc(sizeof(double)*(dim))
if y==NULL:
y = <double*> sig_malloc(sizeof(double)*(dim))
if y == NULL:
raise MemoryError("error allocating memory")
result=[]
v=[0]*dim
result = []
v = [0]*dim
cdef gsl_odeiv_step_type * T

for i from 0 <=i< dim: #copy initial conditions into C array
y[i]=self.y_0[i]
for i in range(dim): # copy initial conditions into C array
y[i] = self.y_0[i]

if self.algorithm == "rkf45":
T=gsl_odeiv_step_rkf45
Expand Down Expand Up @@ -502,9 +502,9 @@ class ode_solver():
if not self.scale_abs:
c = gsl_odeiv_control_standard_new(self.error_abs,self.error_rel,self.a,self.a_dydt)
elif hasattr(self.scale_abs,'__len__'):
if len(self.scale_abs)==dim:
if len(self.scale_abs) == dim:
scale_abs_array =<double *> sig_malloc(dim*sizeof(double))
for i from 0 <=i<dim:
for i in range(dim):
scale_abs_array[i]=self.scale_abs[i]
c = gsl_odeiv_control_scaled_new(self.error_abs,self.error_rel,self.a,self.a_dydt,scale_abs_array,dim)

Expand Down Expand Up @@ -551,11 +551,11 @@ class ode_solver():
sig_free(y)
sig_free(scale_abs_array)
raise TypeError("numpoints must be integer")
result.append( (self.t_span[0],self.y_0))
result.append((self.t_span[0], self.y_0))
delta = (self.t_span[1]-self.t_span[0])/(1.0*num_points)
t =self.t_span[0]
t_end=self.t_span[0]+delta
for i from 0<i<=n:
t = self.t_span[0]
t_end = self.t_span[0]+delta
for i in range(1, n + 1):
while (t < t_end):
try:
sig_on()
Expand All @@ -571,17 +571,17 @@ class ode_solver():
sig_free(scale_abs_array)
raise ValueError("error solving")

for j from 0<=j<dim:
for j in range(dim):
v[j]=<double> y[j]
result.append( (t,copy.copy(v)) )
t = t_end
t_end= t+delta

else:
n = len(self.t_span)
result.append((self.t_span[0],self.y_0))
t=self.t_span[0]
for i from 0<i<n:
result.append((self.t_span[0], self.y_0))
t = self.t_span[0]
for i in range(1, n):
t_end=self.t_span[i]
while (t < t_end):
try:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/calculus/riemann.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ cpdef analytic_boundary(FLOAT_T t, int n, FLOAT_T epsilon):
"""
cdef FLOAT_T i
cdef FLOAT_T result = t
for i from 1 <= i < n+1:
for i in range(1, n + 1):
result += (2*(-1)**i/i)*(epsilon**i/(1+epsilon**(2*i)))*sin(2*i*t)
return result

Expand Down
2 changes: 1 addition & 1 deletion src/sage/calculus/transforms/dwt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ cdef class DiscreteWaveletTransform(GSLDoubleArray):
x_min = 0
if xmax is None:
x_max = self.n
for i from x_min <= i < x_max:
for i in range(x_min, x_max):
x = self.data[i]
if i > 0:
v.append(point([(i, x)], hue=(1, 1, 1), **args))
Expand Down
8 changes: 4 additions & 4 deletions src/sage/calculus/transforms/fft.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ AUTHORS:
- L.F. Tabera Alonso (2013-3): Documentation
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2006 William Stein <wstein@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from cysignals.memory cimport sig_malloc, sig_free

Expand Down Expand Up @@ -257,7 +257,7 @@ cdef class FastFourierTransform_complex(FastFourierTransform_base):
I = I.n()
s = 1/(3*pi) # so arg gets scaled between -1/3 and 1/3.

for i from xmin <= i < xmax:
for i in range(xmin, xmax):
z = self.data[2*i] + I*self.data[2*i+1]
mag = z.abs()
arg = z.arg()*s
Expand Down
Loading