Files
amspyigrf13/scratch/legendre.sage.py
2025-07-18 11:25:48 -04:00

97 lines
2.9 KiB
Python

# This file was *autogenerated* from the file ./legendre.sage
from sage.all_cmdline import * # import sage library
_sage_const_2 = Integer(2); _sage_const_1 = Integer(1); _sage_const_0 = Integer(0); _sage_const_20 = Integer(20); _sage_const_1En14 = RealNumber('1E-14')#!/usr/bin/sage
# Schmidt Quasi-Normalized Legendre Functions for Spherical Harmonics
x = var('x');
def polypart(n,m):
pol = (x**_sage_const_2 -_sage_const_1 )**n
for I in range(_sage_const_1 ,n+_sage_const_1 ):
pol = diff(pol,x)
for I in range(_sage_const_1 ,m+_sage_const_1 ):
pol = diff(pol,x)
return pol
def normpart(n,m):
if(m==_sage_const_0 ):
nrm = _sage_const_1 /_sage_const_2 **n/factorial(n)
else:
nrm = (_sage_const_1 /_sage_const_2 **n/factorial(n)) * sqrt(_sage_const_2 *factorial(n-m)/factorial(n+m))
return nrm
NN = _sage_const_20
lns = []
coefs = []
nmaxcoefs = _sage_const_0
for I in range(_sage_const_0 ,NN+_sage_const_1 ):
for J in range(_sage_const_0 ,I+_sage_const_1 ):
pol = polypart(I,J)
nrm = normpart(I,J)
pol2 = expand(pol)
p2c = pol2.coefficients(sparse=False)
pol3 = nrm*pol2
p3c = pol3.coefficients(sparse=False)
for K in range(_sage_const_0 ,len(p3c)):
p3c[K] = p3c[K].n()
coefs.append(p3c)
if(nmaxcoefs<len(p3c)):
nmaxcoefs = len(p3c)
#print("{:d}\t{:d}\t{}\t\t{}".format(I,J,nrm,p2c))
s = ""
s = s + "{:d}\t{:d}\t".format(I,J)
for K in range(_sage_const_0 ,len(p3c)):
nn = p3c[K]
if(abs(nn)<_sage_const_1En14 ):
nn = _sage_const_0
s = s + "{:1.8g}\t".format(nn)
s = s + "\n"
print(s)
lns.append(s)
##C style coefficient output
fout = "c_legendrecoefs.c"
fp = open(fout,"w+")
pref1 = "static const int amslegendre_ndegree = {:d};\n".format(NN)
pref2 = "static const int amslegendre_maxporder = {:d};\n".format(nmaxcoefs-_sage_const_1 )
pref = pref1 + pref2 + "static const float amslegendrecoefs[] = { \n"
for I in range(_sage_const_0 ,NN+_sage_const_1 ):
for J in range(_sage_const_0 ,I+_sage_const_1 ):
ind = I*(I+_sage_const_1 )/_sage_const_2 +J
for K in range(_sage_const_0 ,nmaxcoefs):
if(K<len(coefs[ind])):
nn = coefs[ind][K]
if(abs(nn)<_sage_const_1En14 ):
nn = _sage_const_0
else:
nn = _sage_const_0
if(ind<len(coefs)-_sage_const_1 ):
if(K<nmaxcoefs-_sage_const_1 ):
pref = pref + "{:1.8g},".format(nn)
else:
pref = pref + "{:1.8g},\n".format(nn)
else:
if(K<nmaxcoefs-_sage_const_1 ):
pref = pref + "{:1.8g},".format(nn)
else:
pref = pref + "{:1.8g}".format(nn) + "\n};\n\n"
fp.writelines(pref)
fp.close()