The problem is to have a real logarithm function of a certain base that
doesn't transform "it self" into log(x)/log(base).
We try to study the implementation of the Sage "log" functions [1] and the
"coercion" model [2] but all of this seems to complex for this simple
problem.
The solution we got is below and uses:
1. A sage "formal function":
LOG_ = function('logb', x, b, print_latex_func=_LOG_latex) 2. A latex way
to express this function: def _LOG_latex(fun,x,base=None): 3. An algorithm
implemented as a python "def" function. def logb(x,base=e,factorize=False) that
returns the "formal function" as an answer. Is this the proper way to do it
in Sage ?
Could it be better and simple?
Thank you,
Pedro Cruz
[1] http://www.sagemath.org/doc/reference/functions/sage/functions/log.html
[2] http://www.sagemath.org/doc/reference/coercion/
About the subject: in some moment, in the portuguese high schools, log
properties are studied keeping the base.
#=======================
# log for "high school"
#=======================
def _LOG_latex(fun,x,base=None):
if b==e or b is None:
return r'\ln(%s)' % latex(x)
else:
return r'\log_{%s}(%s)' % (latex(base),latex(x))
x,b=SR.var('x,b')
LOG_ = function('logb', x, b, print_latex_func=_LOG_latex)
def logb(x,base=e,factorize=False):
r"""logb is an alternative to ``log`` from Sage. This new one keeps the
base.
Usually ``log(105,base=10)`` is transformed by Sage (and many others)
into ``log(105)/log(10)`` and sometimes this is not what we want to see
as
a result.
The latex representation used ``\log_{base} (arg)``.
INPUT:
- ``x`` - the argument of log.
- ``base`` - the base of logarithm.
- ``factorize`` - decompose in a simple expression if argument if
decomposable in prime factors.
OUTPUT:
- an expression based on ``logb``, Sage ``log`` or any other expression.
Basic cases::
sage: logb(e) #assume base=e
1
sage: logb(10,base=10)
1
sage: logb(1) #assume base=e
0
sage: logb(1,base=10) #assume base=e
0
sage: logb(e,base=10)
logb(e, 10)
sage: logb(10,base=e) #converted to Sage "log" function
log(10)
sage: logb(sqrt(105)) #again, converted to Sage "log" function
log(sqrt(105))
With and without factorization::
sage: logb(3^5,base=10) #no factorization
logb(243, 10)
sage: logb(3^5,base=10,factorize=True)
5*logb(3, 10)
sage: logb(3^5*2^3,base=10) #no factorization
logb(1944, 10)
sage: logb(3^5*2^3,base=10,factorize=True)
5*logb(3, 10) + 3*logb(2, 10)
Latex printing of logb::
sage: latex( logb(e) )
1
sage: latex( logb(1,base=10) )
0
sage: latex( logb(sqrt(105)) )
\log\left(\sqrt{105}\right)
sage: latex( logb(3^5,base=10) )
\log_{10}(243)
sage: latex( logb(3^5,base=10,factorize=True) )
5 \, \log_{10}(3)
sage: latex( logb(3^5*2^3,base=10,factorize=True) )
5 \, \log_{10}(3) + 3 \, \log_{10}(2)
"""
#e is exp(1) in sage
r = log(x,base=base)
if SR(r).denominator()==1:
return r
else:
if factorize:
F = factor(x)
if factorize and type(F) == sage.structure.factorization_integer.
IntegerFactorization:
l = [ factor_exponent * LOG_(x=factor_base,b=base) for (
factor_base,factor_exponent) in F ]
return add(l)
else:
return LOG_(x=x,b=base)
--
You received this message because you are subscribed to the Google Groups
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.