Git Product home page Git Product logo

Comments (6)

odashi avatar odashi commented on April 28, 2024 1

Yes indeed. This kind of modification should be optional, or it will include unintended changes, e.g.:

  • xi ... \xi or x_i
  • y_hat ... \hat{y} or y_{hat}

Especially the second case is quite troublesome, as the underscore (_) completely behaves differently.

For the interface, some optional parameter of with_latex/get_latex would help to switch the behavior:

@with_latex(math_symbol=True, diacritic=True)
def foobar(...):
  ...

I think math_symbol is okay to add, but consider diacritic is not for now. Probably we need to support subscripts before diacritics.

from latexify_py.

ryxcommar avatar ryxcommar commented on April 28, 2024 1

I also suggest the following:

# Old:
ast.Mult: (lambda: _wrap(l) + _wrap(r)),

# New:
ast.Mult: (lambda: _wrap(l) + ' ' + _wrap(r)),

Otherwise this does not work:

function(beta, x):
    return beta * x

^ That example is why I use return '\\' + val + ' ':

beta * x --> \betax (bad)

versus

beta * x --> \beta x (good)

If you do ast.Mult: (lambda: _wrap(l) + ' ' + _wrap(r)), instead, then it is no longer necessary to do '\\' + val + ' '; you will only need '\\' + val

from latexify_py.

odashi avatar odashi commented on April 28, 2024 1

SGTM for the behavior of the math_symbol.

You can insert braces as symbol separators: {\beta}{x}. I think every identifier should be put in a brace for safety.

from latexify_py.

ryxcommar avatar ryxcommar commented on April 28, 2024

Something like this?

Behaviors:

image

Code:

# ...
from typing import Callable

# ...

class LatexifyVisitor(ast.NodeVisitor):

  def __init__(self, math_symbol=True):
    self.math_symbol = math_symbol
    super(ast.NodeVisitor).__init__()

  def _parse_math_symbols(self, val: str) -> str:
    if not self.math_symbol:
      return val
    else:
      greek_and_hebrew = [
          'aleph', 'alpha', 'beta', 'beth', 'chi', 'daleth',
          'delta', 'digamma', 'epsilon', 'eta', 'gamma', 'gimel',
          'iota', 'kappa', 'lambda', 'mu', 'nu', 'omega', 'omega',
          'phi', 'pi', 'psi', 'rho', 'sigma', 'tau', 'theta',
          'upsilon', 'varepsilon', 'varkappa', 'varphi', 'varpi', 'varrho',
          'varsigma',
          'vartheta', 'xi', 'zeta'
      ]
      if val.lower() in greek_and_hebrew:
        return '\\' + val + ' '
      else:
        return val

  # ........

  def visit_FunctionDef(self, node):
    name_str = r'\operatorname{' + str(node.name) + '}'
    arg_strs = [self._parse_math_symbols(str(arg.arg)) for arg in node.args.args]
    body_str = self.visit(node.body[0])
    return name_str + '(' + ', '.join(arg_strs) + r') \triangleq ' + body_str

  # ........

  def visit_Name(self, node):
    return self._parse_math_symbols(str(node.id))

# ........

def get_latex(fn, math_symbol=True):
  return LatexifyVisitor(math_symbol=math_symbol).visit(ast.parse(inspect.getsource(fn)))

# ........

def with_latex(*args, math_symbol=True):

  class _LatexifiedFunction:
    def __init__(self, fn):
      self._fn = fn
      self._str = get_latex(fn, math_symbol=math_symbol)

  # ........

  if len(args) == 1 and isinstance(args[0], Callable):
    return _LatexifiedFunction(args[0])
  else:
    return lambda fn: _LatexifiedFunction(fn)

from latexify_py.

ryxcommar avatar ryxcommar commented on April 28, 2024

Need to be careful with wrapping everything in {}. I realized if you do that for every self.visit_Name, you get this:

image

I am not good with the ast library, so I'm not sure how you would do it 100% correctly. For now I am going to be safe, and only convert a name if it is in greek_and_hebrew.

from latexify_py.

odashi avatar odashi commented on April 28, 2024

Yes. I am considering a better way to traverse ASTs and will change them in some future commits.

from latexify_py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.