Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trying to avoid "import sage"
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Chapoton committed Nov 30, 2018
1 parent b36eca1 commit 55cc8b6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 59 deletions.
66 changes: 38 additions & 28 deletions src/sage/combinat/finite_state_machine.py
Expand Up @@ -937,8 +937,17 @@
import collections
import itertools

import sage
from sage.calculus.var import var
from sage.functions.trig import atan2
from sage.graphs.digraph import DiGraph
from sage.matrix.constructor import matrix
from sage.misc.cachefunc import cached_function
from sage.misc.latex import latex
from sage.misc.misc import verbose
from sage.misc.sageinspect import sage_getargspec
from sage.rings.qqbar import QQbar
from sage.rings.real_mpfr import RR
from sage.structure.sage_object import SageObject


def full_group_by(l, key=lambda x: x):
Expand Down Expand Up @@ -1164,7 +1173,7 @@ def is_FSMState(S):
return isinstance(S, FSMState)


class FSMState(sage.structure.sage_object.SageObject):
class FSMState(SageObject):
"""
Class for a state of a finite state machine.

Expand Down Expand Up @@ -2221,7 +2230,7 @@ def is_FSMTransition(T):
return isinstance(T, FSMTransition)


class FSMTransition(sage.structure.sage_object.SageObject):
class FSMTransition(SageObject):
"""
Class for a transition of a finite state machine.

Expand Down Expand Up @@ -2710,7 +2719,7 @@ def duplicate_transition_add_input(old_transition, new_transition):
return old_transition


class FiniteStateMachine(sage.structure.sage_object.SageObject):
class FiniteStateMachine(SageObject):
"""
Class for a finite state machine.

Expand Down Expand Up @@ -4357,7 +4366,7 @@ def _repr_(self):
else:
return "Finite state machine with %s states" % len(self._states_)

default_format_letter = sage.misc.latex.latex
default_format_letter = latex
format_letter = default_format_letter


Expand Down Expand Up @@ -4391,7 +4400,7 @@ def format_letter_negative(self, letter):
if letter in ZZ and letter < 0:
return r'\overline{%d}' % -letter
else:
return sage.misc.latex.latex(letter)
return latex(letter)


def format_transition_label_reversed(self, word):
Expand Down Expand Up @@ -4877,7 +4886,7 @@ def latex_options(self,
state.accepting_where = where
elif hasattr(state, 'final_word_out') \
and state.final_word_out:
if where in sage.rings.real_mpfr.RR:
if where in RR:
state.accepting_where = where
else:
raise ValueError('accepting_where for %s must '
Expand All @@ -4891,7 +4900,6 @@ def latex_options(self,
if accepting_show_empty is not None:
self.accepting_show_empty = accepting_show_empty


def _latex_(self):
r"""
Returns a LaTeX code for the graph of the finite state machine.
Expand Down Expand Up @@ -5020,7 +5028,7 @@ def label_rotation(angle, both_directions):
elif hasattr(self, "format_state_label"):
label = self.format_state_label(vertex)
else:
label = sage.misc.latex.latex(vertex.label())
label = latex(vertex.label())
result += "\\node[state%s] (v%d) at (%f, %f) {$%s$};\n" % (
options, j, vertex.coordinates[0],
vertex.coordinates[1], label)
Expand Down Expand Up @@ -5067,7 +5075,7 @@ def key_function(s):
transition, self.format_transition_label))
label = ", ".join(labels)
if source != target:
angle = sage.functions.trig.atan2(
angle = atan2(
target.coordinates[1] - source.coordinates[1],
target.coordinates[0] - source.coordinates[0]) * 180/pi
both_directions = (target, source) in adjacent
Expand Down Expand Up @@ -5098,7 +5106,7 @@ def key_function(s):


def _latex_transition_label_(self, transition,
format_function=sage.misc.latex.latex):
format_function=None):
r"""
Returns the proper transition label.

Expand Down Expand Up @@ -5287,7 +5295,7 @@ def adjacency_matrix(self, input=None,
from sage.rings.integer_ring import ZZ

def default_function(transitions):
x = sage.symbolic.ring.SR.var('x')
x = var('x')
return x**sum(transition.word_out)

if entry is None:
Expand All @@ -5312,7 +5320,7 @@ def default_function(transitions):
dictionary[(transition.from_state.label(),
transition.to_state.label())] \
= entry(transition)
return sage.matrix.constructor.matrix(
return matrix(
len(relabeledFSM.states()), dictionary)


Expand Down Expand Up @@ -9247,7 +9255,7 @@ def find_common_output(state):
+ [common_output[0]]
found_inbound_transition = True
if not found_inbound_transition:
sage.misc.misc.verbose(
verbose(
"All transitions leaving state %s have an "
"output label with prefix %s. However, "
"there is no inbound transition and it is "
Expand All @@ -9258,7 +9266,6 @@ def find_common_output(state):
(state, common_output[0]),
level=0)


def equivalence_classes(self):
r"""
Returns a list of equivalence classes of states.
Expand Down Expand Up @@ -10006,7 +10013,7 @@ def graph(self, edge_labels='words_in_out'):
graph_data.append((t.from_state.label(), t.to_state.label(),
label_fct(t)))

G = sage.graphs.digraph.DiGraph(graph_data, multiedges=True, loops=True)
G = DiGraph(graph_data, multiedges=True, loops=True)
G.add_vertices(isolated_vertices)
return G

Expand Down Expand Up @@ -10093,8 +10100,8 @@ def predecessors(self, state, valid_input=None):
return(done)


def number_of_words(self, variable=sage.symbolic.ring.SR.var('n'),
base_ring=sage.rings.qqbar.QQbar):
def number_of_words(self, variable=var('n'),
base_ring=None):
r"""
Return the number of successful input words of given length.

Expand Down Expand Up @@ -10204,11 +10211,12 @@ def number_of_words(self, variable=sage.symbolic.ring.SR.var('n'),
...
NotImplementedError: Finite State Machine must be deterministic.
"""
from sage.matrix.constructor import matrix
from sage.modules.free_module_element import vector
from sage.arith.all import falling_factorial
from sage.rings.integer_ring import ZZ
from sage.symbolic.ring import SR
if base_ring is None:
base_ring = QQbar

def jordan_block_power(block, exponent):
eigenvalue = SR(block[0, 0])
Expand All @@ -10232,8 +10240,7 @@ def jordan_block_power(block, exponent):
left_T = (left * T).change_ring(SR)
return left_T * Jpower * T_inv_right


def asymptotic_moments(self, variable=sage.symbolic.ring.SR.var('n')):
def asymptotic_moments(self, variable=var('n')):
r"""
Returns the main terms of expectation and variance of the sum
of output labels and its covariance with the sum of input
Expand Down Expand Up @@ -10657,7 +10664,7 @@ def get_matrix(fsm, x, y):
try:
M = get_matrix(self, x, y)
except (TypeError, ValueError):
sage.misc.misc.verbose(
verbose(
"Non-integer output weights lead to "
"significant performance degradation.", level=0)
# fall back to symbolic ring
Expand Down Expand Up @@ -11338,7 +11345,7 @@ def _repr_(self):


def _latex_transition_label_(self, transition,
format_function=sage.misc.latex.latex):
format_function=None):
r"""
Returns the proper transition label.

Expand Down Expand Up @@ -11369,6 +11376,8 @@ def _latex_transition_label_(self, transition,
sage: F._latex_transition_label_(t)
\left[0\right]
"""
if format_function is None:
format_function = latex
return format_function(transition.word_in)


Expand Down Expand Up @@ -12547,7 +12556,7 @@ def _repr_(self):


def _latex_transition_label_(self, transition,
format_function=sage.misc.latex.latex):
format_function=None):
r"""
Returns the proper transition label.

Expand Down Expand Up @@ -12578,6 +12587,8 @@ def _latex_transition_label_(self, transition,
sage: F._latex_transition_label_(t)
\left[0\right] \mid \left[1\right]
"""
if format_function is None:
format_function = latex
return (format_function(transition.word_in) + "\\mid "
+ format_function(transition.word_out))

Expand Down Expand Up @@ -13350,7 +13361,7 @@ def _process_convert_output_(self, output_data, **kwargs):
#*****************************************************************************


class _FSMTapeCache_(sage.structure.sage_object.SageObject):
class _FSMTapeCache_(SageObject):
"""
This is a class for caching an input tape. It is used in
:class:`FSMProcessIterator`.
Expand Down Expand Up @@ -14241,7 +14252,7 @@ def is_FSMProcessIterator(PI):
#*****************************************************************************


class FSMProcessIterator(sage.structure.sage_object.SageObject,
class FSMProcessIterator(SageObject,
collections.Iterator):
"""
This class takes an input, feeds it into a finite state machine
Expand Down Expand Up @@ -15561,7 +15572,7 @@ def __init__(self, *args, **kwargs):
#*****************************************************************************


@sage.misc.cachefunc.cached_function
@cached_function
def setup_latex_preamble():
r"""
This function adds the package ``tikz`` with support for automata
Expand All @@ -15586,7 +15597,6 @@ def setup_latex_preamble():
sage: (r"\usepackage{tikz}" in latex.extra_preamble()) == latex.has_file("tikz.sty")
True
"""
from sage.misc.latex import latex
latex.add_package_to_preamble_if_available('tikz')
latex.add_to_mathjax_avoid_list("tikz")
if latex.has_file("tikz.sty"):
Expand Down
5 changes: 3 additions & 2 deletions src/sage/geometry/all.py
Expand Up @@ -20,11 +20,12 @@

from . import toric_plotter


from .hyperbolic_space.all import *

from .voronoi_diagram import *
from .voronoi_diagram import VoronoiDiagram

lazy_import('sage.geometry.ribbon_graph', 'RibbonGraph')
lazy_import('sage.geometry.hyperplane_arrangement.arrangement', 'HyperplaneArrangements')
lazy_import('sage.geometry.hyperplane_arrangement.library', 'hyperplane_arrangements')

del absolute_import
3 changes: 3 additions & 0 deletions src/sage/groups/additive_abelian/all.py
@@ -1,3 +1,6 @@
from __future__ import absolute_import

from .additive_abelian_group import AdditiveAbelianGroup
from .additive_abelian_wrapper import *

del absolute_import
12 changes: 4 additions & 8 deletions src/sage/rings/complex_mpc.pyx
Expand Up @@ -46,7 +46,7 @@ EXAMPLES::
sage: MPC("infinity + NaN *I")
+infinity + NaN*I
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2008 Philippe Theveny <thevenyp@loria.fr>
# 2008 Alex Ghitza
# 2010 Yann Laigle-Chapuy
Expand All @@ -55,20 +55,17 @@ EXAMPLES::
# Distributed under the terms of the GNU General Public License (GPL)
# 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 __future__ import absolute_import, print_function


import sage
import re
from . import real_mpfr
import weakref
from cpython.object cimport Py_NE

import sage
from sage.cpython.string cimport str_to_bytes

from sage.libs.mpfr cimport *
from sage.libs.mpc cimport *
from sage.structure.parent cimport Parent
Expand Down Expand Up @@ -2627,4 +2624,3 @@ cdef class CCtoMPC(Map):
y = (<MPComplexField_class>self.codomain())._new()
mpc_set_fr_fr(y.value, (<ComplexNumber>z).__re, (<ComplexNumber>z).__im, rnd)
return y

12 changes: 6 additions & 6 deletions src/sage/rings/noncommutative_ideals.pyx
Expand Up @@ -47,24 +47,24 @@ TESTS::
running ._test_not_implemented_methods() . . . pass
running ._test_pickling() . . . pass
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2011 Simon King <simon.king@uni-jena.de>
#
# 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 __future__ import absolute_import

from sage.structure.element cimport MonoidElement
from sage.structure.parent cimport Parent
from sage.categories.monoids import Monoids
from sage.rings.ideal_monoid import IdealMonoid_c
from sage.rings.ideal import Ideal_generic
import sage

from sage.rings.integer_ring import ZZ


class IdealMonoid_nc(IdealMonoid_c):
Expand Down Expand Up @@ -101,7 +101,7 @@ class IdealMonoid_nc(IdealMonoid_c):
"""
self._IdealMonoid_c__R = R
Parent.__init__(self, base=sage.rings.integer_ring.ZZ,
Parent.__init__(self, base=ZZ,
category=Monoids())
self._populate_coercion_lists_()

Expand Down
7 changes: 3 additions & 4 deletions src/sage/sets/finite_set_map_cy.pyx
Expand Up @@ -45,15 +45,14 @@ AUTHORS:
- Florent Hivert
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2010 Florent Hivert <Florent.Hivert@univ-rouen.fr>,
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import absolute_import

import sage
from sage.structure.list_clone cimport ClonableIntArray
from sage.structure.parent cimport Parent
from sage.arith.power cimport generic_power
Expand Down

0 comments on commit 55cc8b6

Please sign in to comment.