log for high school

30 views
Skip to first unread message

Pedro Cruz

unread,
Nov 5, 2013, 5:53:35 AM11/5/13
to sage-s...@googlegroups.com
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



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)


kcrisman

unread,
Nov 5, 2013, 12:13:00 PM11/5/13
to sage-s...@googlegroups.com


On Tuesday, November 5, 2013 5:53:35 AM UTC-5, Pedro Cruz wrote:
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?



This looks pretty useful!  I wonder whether it could find a home in Sage, though one would have to make sure that it didn't conflict with something else.

The "best" way to do this is to use BuiltinFunction like at sage/functions/hyperbolic.py or the like... obviously for your own purposes this is just fine.  But that one has a nice way to put in typesetting etc.

Good luck!
- kcrisman 
Reply all
Reply to author
Forward
0 new messages