Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoeppe committed Oct 29, 2021
2 parents 543d5bf + 238e97e commit 94a81a9
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 227 deletions.
8 changes: 3 additions & 5 deletions src/sage/coding/code_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@
from sage.rings.all import QQ, RR, ZZ, RDF
from sage.arith.misc import is_prime_power
from sage.arith.all import binomial
from sage.misc.lazy_import import lazy_import
lazy_import("sage.functions.log", "log")
from sage.misc.functional import sqrt
from sage.misc.functional import sqrt, log
from .delsarte_bounds import (delsarte_bound_hamming_space,
delsarte_bound_additive_hamming_space)

Expand Down Expand Up @@ -583,9 +581,9 @@ def entropy(x, q=2):
sage: codes.bounds.entropy(0, 2)
0
sage: codes.bounds.entropy(1/5,4).factor()
sage: codes.bounds.entropy(1/5,4).factor() # optional - sage.symbolic
1/10*(log(3) - 4*log(4/5) - log(1/5))/log(2)
sage: codes.bounds.entropy(1, 3)
sage: codes.bounds.entropy(1, 3) # optional - sage.symbolic
log(2)/log(3)
Check that values not within the limits are properly handled::
Expand Down
2 changes: 1 addition & 1 deletion src/sage/coding/kasami_codes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class KasamiCode(AbstractLinearCode):
sage: C.minimum_distance()
3
"""
from sage.functions.log import log
from sage.misc.functional import log

m = log(self._s, 2)
F = GF(self._s)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/words/finite_word.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def topological_entropy(self, n):
if n == 0:
return 1
pn = self.number_of_factors(n)
from sage.functions.all import log
from sage.functions.log import log
return log(pn, base=d)/n

def rauzy_graph(self, n):
Expand Down
183 changes: 2 additions & 181 deletions src/sage/functions/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ

from sage.misc.functional import log as log


class Function_exp(GinacFunction):
r"""
Expand Down Expand Up @@ -274,187 +276,6 @@ def __init__(self):
logb = Function_log2()


def log(*args, **kwds):
"""
Return the logarithm of the first argument to the base of
the second argument which if missing defaults to ``e``.
It calls the ``log`` method of the first argument when computing
the logarithm, thus allowing the use of logarithm on any object
containing a ``log`` method. In other words, ``log`` works
on more than just real numbers.
EXAMPLES::
sage: log(e^2)
2
To change the base of the logarithm, add a second parameter::
sage: log(1000,10)
3
The synonym ``ln`` can only take one argument::
sage: ln(RDF(10))
2.302585092994046
sage: ln(2.718)
0.999896315728952
sage: ln(2.0)
0.693147180559945
sage: ln(float(-1))
3.141592653589793j
sage: ln(complex(-1))
3.141592653589793j
You can use
:class:`RDF<sage.rings.real_double.RealDoubleField_class>`,
:class:`~sage.rings.real_mpfr.RealField` or ``n`` to get a
numerical real approximation::
sage: log(1024, 2)
10
sage: RDF(log(1024, 2))
10.0
sage: log(10, 4)
1/2*log(10)/log(2)
sage: RDF(log(10, 4))
1.6609640474436813
sage: log(10, 2)
log(10)/log(2)
sage: n(log(10, 2))
3.32192809488736
sage: log(10, e)
log(10)
sage: n(log(10, e))
2.30258509299405
The log function works for negative numbers, complex
numbers, and symbolic numbers too, picking the branch
with angle between `-\\pi` and `\\pi`::
sage: log(-1+0*I)
I*pi
sage: log(CC(-1))
3.14159265358979*I
sage: log(-1.0)
3.14159265358979*I
Small integer powers are factored out immediately::
sage: log(4)
2*log(2)
sage: log(1000000000)
9*log(10)
sage: log(8) - 3*log(2)
0
sage: bool(log(8) == 3*log(2))
True
The ``hold`` parameter can be used to prevent automatic evaluation::
sage: log(-1,hold=True)
log(-1)
sage: log(-1)
I*pi
sage: I.log(hold=True)
log(I)
sage: I.log(hold=True).simplify()
1/2*I*pi
For input zero, the following behavior occurs::
sage: log(0)
-Infinity
sage: log(CC(0))
-infinity
sage: log(0.0)
-infinity
The log function also works in finite fields as long as the
argument lies in the multiplicative group generated by the base::
sage: F = GF(13); g = F.multiplicative_generator(); g
2
sage: a = F(8)
sage: log(a,g); g^log(a,g)
3
8
sage: log(a,3)
Traceback (most recent call last):
...
ValueError: no logarithm of 8 found to base 3 modulo 13
sage: log(F(9), 3)
2
The log function also works for p-adics (see documentation for
p-adics for more information)::
sage: R = Zp(5); R
5-adic Ring with capped relative precision 20
sage: a = R(16); a
1 + 3*5 + O(5^20)
sage: log(a)
3*5 + 3*5^2 + 3*5^4 + 3*5^5 + 3*5^6 + 4*5^7 + 2*5^8 + 5^9 +
5^11 + 2*5^12 + 5^13 + 3*5^15 + 2*5^16 + 4*5^17 + 3*5^18 +
3*5^19 + O(5^20)
TESTS:
Check if :trac:`10136` is fixed::
sage: ln(x).operator() is ln
True
sage: log(x).operator() is ln
True
sage: log(1000, 10)
3
sage: log(3,-1)
-I*log(3)/pi
sage: log(int(8),2)
3
sage: log(8,int(2))
3
sage: log(8,2)
3
sage: log(1/8,2)
-3
sage: log(1/8,1/2)
3
sage: log(8,1/2)
-3
sage: log(1000, 10, base=5)
Traceback (most recent call last):
...
TypeError: Symbolic function log takes at most 2 arguments (3 given)
Check if :trac:`29164` is fixed::
sage: log(0, 2)
-Infinity
"""
base = kwds.pop('base', None)
if base:
args = args + (base,)
if not args:
raise TypeError("Symbolic function log takes at least 1 arguments (0 given)")
if len(args) == 1:
return ln(args[0], **kwds)
if len(args) > 2:
raise TypeError("Symbolic function log takes at most 2 arguments (%s given)" % (len(args) + 1 - (base is not None)))
try:
return args[0].log(args[1])
except ValueError as ex:
if ex.args[0].startswith("no logarithm"):
raise
return logb(args[0], args[1])
except (AttributeError, TypeError):
return logb(args[0], args[1])


class Function_polylog(GinacFunction):
def __init__(self):
r"""
Expand Down
7 changes: 3 additions & 4 deletions src/sage/graphs/generators/distance_regular.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ def is_classical_parameters_graph(list array):
sage: is_classical_parameters_graph([27, 10, 1, 1, 10, 27])
False
"""
from sage.functions.log import log
from sage.misc.functional import log
from sage.rings.integer_ring import ZZ
from sage.arith.misc import is_prime_power
from sage.combinat.q_analogues import q_binomial
Expand Down Expand Up @@ -2099,8 +2099,7 @@ def graph_with_classical_parameters(int d, int b, alpha_in, beta_in, int gamma):
Generalised hexagon of order (2, 8): Graph on 819 vertices
"""
from sage.rings.rational import Rational
from sage.functions.log import log
from sage.misc.functional import sqrt
from sage.misc.functional import sqrt, log
from sage.graphs.generators.families import JohnsonGraph, HammingGraph
from sage.graphs.generators.classical_geometries import \
UnitaryDualPolarGraph, OrthogonalDualPolarGraph, SymplecticDualPolarGraph
Expand Down Expand Up @@ -2383,7 +2382,7 @@ def is_near_polygon(array):
"""
from sage.arith.misc import is_prime_power
from sage.combinat.q_analogues import q_binomial
from sage.functions.log import log
from sage.misc.functional import log

if len(array) % 2 != 0:
return False
Expand Down
4 changes: 2 additions & 2 deletions src/sage/matrix/operation_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ def _name_maker(self, names):
...
ValueError: element names must be a list, or one of the keywords: 'letters', 'digits', 'elements'
"""
from sage.functions.log import log
name_list=[]
from sage.misc.functional import log
name_list = []
if names == 'digits':
if self._n == 0 or self._n == 1:
width = 1
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/dev_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def import_statements(*objects, **kwds):
:trac:`23779`)::
sage: import_statements('log')
from sage.functions.log import log
from sage.misc.functional import log
.. NOTE::
Expand Down
Loading

0 comments on commit 94a81a9

Please sign in to comment.