Ask Your Question

Woodgnome's profile - activity

2017-09-20 10:21:55 -0500 received badge  Famous Question (source)
2017-02-26 21:21:26 -0500 received badge  Popular Question (source)
2016-07-24 19:57:54 -0500 received badge  Famous Question (source)
2016-07-24 19:57:54 -0500 received badge  Notable Question (source)
2016-01-09 11:31:14 -0500 received badge  Notable Question (source)
2016-01-09 11:31:14 -0500 received badge  Popular Question (source)
2015-04-01 14:15:23 -0500 received badge  Popular Question (source)
2015-03-20 02:35:49 -0500 received badge  Nice Question (source)
2015-03-18 04:00:47 -0500 asked a question Why can Sage solve one log equation, but not another with only different values?
solve(3 == 586 * 0.2 ^ x, x)
[x == log(586/3)/log(5)] // Numerical: 3.27736003989833

solve(3 == 586 * 0.99557 ^ x, x)
[100000^x == 586/3*99557^x] // Wat???

Why is it not capable of solving the second equation? Math tells us there is a solution and that it is:

log(3/586) / log(0.99557) = 1188.03958878842
2015-01-14 03:02:36 -0500 received badge  Famous Question (source)
2014-10-07 06:29:16 -0500 received badge  Notable Question (source)
2014-10-07 06:29:16 -0500 received badge  Popular Question (source)
2014-07-23 10:39:41 -0500 answered a question how to run sage as a service

While there's an accepted answer for this question it's also worth checking out http://ask.sagemath.org/question/2350...

2014-07-21 13:46:40 -0500 commented answer Any way to get numeric result of part of symbolic expresssion?

Not sure I understand this, but can your principle still be applied with my latest of the question?

2014-07-21 13:46:16 -0500 commented answer Any way to get numeric result of part of symbolic expresssion?

I've edited my question for clarity.

2014-07-21 09:53:33 -0500 asked a question Any way to get numeric result of part of symbolic expresssion?

If I have a symbolic expression sqrt(2) + x, is it possible to get this to show as 1,414.... + x?

Edit:

I should probably add that my symbolic expression can be any arbitrary composition of numbers and symbols.

2014-07-21 02:38:02 -0500 asked a question How to run Sage as a daemon / service?

This isn't really a question as much as it is an attempt to get the "Sage daemon/service" keywords indexed in search engines (I came upon many people asking for this in my own search).

I figured out how to run Sage as a daemon using Python and sockets - the code can be found in http://ask.sagemath.org/question/2343... .

I was looking into creating an init script, but my Linux knowledge is too lacking in this area (even when I try to edit the /etc/init.d/skeleton script). In any case anyone who can write a proper init script this would be very welcome - I was imagining something along the lines of an init script calling a binary /usr/bin/sageadmin that would:

  • Start daemon via Sage executable and path to the Python daemon script
  • Stop daemon using sockets
  • Check status using sockets

As for the sageadminbinary I can write the socket C code, but I'm not sure what else is required. http://www.enderunix.org/docs/eng/dae... seems to have a couple of good points, but again - I'm not that comfortable with writing code for something this low level in Linux.

So to sum up: I've made the Python script for "daemonizing" Sage - I'm hoping someone else (with more complete Linux knowledge) can write an init script and the required parts of a sageadmin binary.

2014-07-18 09:52:28 -0500 received badge  Scholar (source)
2014-07-18 09:52:28 -0500 commented answer Can't run Python script in Sage, error at import sage.all

I just realised what you meant - being stupid here. Renaming it fixed it, cheers!

2014-07-18 02:38:37 -0500 commented answer Can't run Python script in Sage, error at import sage.all

In the case of `sage -python [PATH TO PYTHON FILE]` `socket.py` is the actual Python script. In the case of `sage [PATH TO SAGE FILE]` Sage compiles the `socket.sage` to a Python file called - you guess it - `socket.py`! Removing this file would be the same as removing the entire script.

2014-07-17 09:56:59 -0500 received badge  Editor (source)
2014-07-17 09:53:53 -0500 asked a question Can't run Python script in Sage, error at import sage.all

I only just figured out how to run Sage in a daemon/service-like kind of state - see http://ask.sagemath.org/question/2343... for the Sage script.

Now I'm trying to rewrite it to Python with Sage imports, so I can automatically start it with sage -python [PATH TO PYTHON FILE] as opposed to manually starting Sage and loading the script.

The fails with the following stack trace

Traceback (most recent call last):
  File "socket.py", line 1, in <module>
    import socket
  File "/var/www/lingapps/assets/sage/socket.py", line 4, in <module>
    import sage.all
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all.py", line 54, in <module>
    from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/env.py", line 84, in <module>
    _add_variable_or_fallback('HOSTNAME',        socket.gethostname())
AttributeError: 'module' object has no attribute 'gethostname'

This also happens when I have just a single line in the script: import sage.all.

It also fails with the following error if I just start sage -python and type import sage.all:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all.py", line 54, in <module>
    from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/env.py", line 20, in <module>
    import os, socket
  File "socket.py", line 6, in <module>
    import sage.calculus.predefined
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/__init__.py", line 1, in <module>
    import all
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/all.py", line 1, in <module>
    from calculus import maxima as maxima_calculus
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/calculus.py", line 400, in <module>
    from sage.rings.all import RR, Integer, CC, QQ, RealDoubleElement, algdep
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/rings/all.py", line 21, in <module>
    from ring import Ring
  File "ring.pyx", line 1, in init sage.rings.ring (sage/rings/ring.c:18156)
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/__init__.py", line 1, in <module>
    import dynamic_class # allows for sage.structure.dynamic_class?
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/dynamic_class.py", line 119, in <module>
    from sage.misc.cachefunc import weak_cached_function
  File "cachefunc.pyx", line 421, in init sage.misc.cachefunc (sage/misc/cachefunc.c:17825)
  File "function_mangling.pyx", line 39, in init sage.misc.function_mangling (sage/misc/function_mangling.c:3106)
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/misc/sageinspect.py", line 131, in <module>
    from sage.env import SAGE_SRC
ImportError: cannot import name SAGE_SRC

It doesn't work with sage [PATH TO SAGE FILE]either:

Traceback (most recent call last):
  File "socket ...
(more)
2014-07-17 07:54:49 -0500 received badge  Self-Learner (source)
2014-07-17 07:54:49 -0500 received badge  Teacher (source)
2014-07-17 07:44:54 -0500 answered a question Running Sage from other languages with high(er) performance?

I finally realised that the Sage shell is a Python shell and that it's possible to run Sage inside a Python script. With this knowledge I was able to write a Python script that can execute Sage commands and uses sockets for inter-process communication.

I decided to write the script to act as much like running sage -c 'commmand' as possible. I copied most of the code from sage/local/sage-eval and paired it up with a Python socket example and ended with the following Python script:

import socket
import sys
from cStringIO import StringIO
from sage.all import *
from sage.calculus.predefined import x
from sage.misc.preparser import preparse

SHUTDOWN = False
HOST = 'localhost'
PORT = 8888
MAX_MSG_LENGTH = 102400

# Create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Socket created'

# Bind socket to localhost and port
try:
  s.bind((HOST, PORT))
except socket.error , msg:
  print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
  sys.exit()

print 'Socket bind complete'

# Start listening on socket
s.listen(10)
print 'Socket now listening'

# Loop listener for new connections
while not SHUTDOWN:
  # Wait to accept a new client connection
  conn, addr = s.accept()
  print 'Connected with ' + addr[0] + ':' + str(addr[1])

  # Receive message from client
  msg = conn.recv(MAX_MSG_LENGTH)
  if msg:
    if msg == "stop":
      SHUTDOWN = True
    else:
      parsed = preparse(msg)
      if parsed.startswith('load') or parsed.startswith('attach'):
        os.system('sage "' + os.path.join(os.getcwd(), parsed.split(None, 1)[1]) + '"')
      else:
        # Redirect stdout to my stdout to capture into a string
        sys.stdout = mystdout = StringIO()

        # Evalutate msg
        try:
          eval(compile(parsed,'<cmdline>','exec'))
          result = mystdout.getvalue() # Get result from mystdout
        except Exception as e:
          result = "ERROR: " + str(type(e)) + " " + str(e)

        # Restore stdout
        sys.stdout = sys.__stdout__

        # Send response to connected client
        if result == "":
          conn.sendall("Empty result, did you remember to print?")
        else:
          conn.sendall(result)

  # Close client connection
  conn.close()

# Close listener
s.close()

I saved the script to a file called sage-daemon.py in my Sage root folder. The stopmessage handler isn't really neccessary, but it's useful if anyone can "properly" daemonize the script (and also to avoid interrupting the script with CTRL+C, because that will leave the socket as in-use for a while after the script has been interrupted). See my other question http://ask.sagemath.org/question/2350... .

To run the script: sage -python sage-daemon.py This will leave the Sage shell waiting for socket input.

Here's a PHP script I used to test the functionality with:

<?php

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
if (!$socket) die("Could not create socket\n");

$connected = socket_connect($socket, "localhost", 8888);
if (!$connected) echo "Not connected\n";
else {
  $msg = "print solve(5*x==3,x)";

  $sent = socket_send($socket, $msg, strlen($msg), 0);
  if (!$sent) echo "Error sending message\n";
  else {
    if ($msg != "stop"){
      $response = socket_read($socket, 1024);
      if (!$response) echo "Error receiving response\n";
      else echo $response . "\n";
    }
  }
}

socket_close($socket);

If successfull this should print ... (more)

2014-07-17 03:29:15 -0500 commented answer Running Sage from other languages with high(er) performance?

sage-forker still seems to work, trying to modify it to accept a `-c` command, but not really having any luck with the Python part of the code :<

2014-07-16 11:59:12 -0500 received badge  Nice Question (source)
2014-07-16 09:43:45 -0500 received badge  Student (source)
2014-07-16 04:46:18 -0500 received badge  Organizer (source)
2014-07-16 04:45:14 -0500 asked a question Running Sage from other languages with high(er) performance?

I'm in the process of creating af CAS tool that uses Sage as the underlying math engine. It's a web application, so most of the code is HTML/Javascript and some underlying PHP from which I'd like to use Sage. Currently I've resorted to the -c command line option and executing it through PHP with exec().

This approach is extremely slow (takes several seconds and CPU load is fairly high). This is - as far as I'm aware - a result of Sage having to load all its libraries each time it's run.

Is there any way I can optimize this approach? I would prefer an approach similar to the -c option where I can just send a command and read the printed result.

I have no issues writing a wrapper in C (or Python if neccessary), nor working with sockets (if any are available) provided it can be done as simple as the -c option (the WebSocket messages seen in Sage Cell seems too much trouble to do manually).

What I need to know is mostly whether or not it's even possible, if so then where to start otherwise I'd like to know if there are any alternative approaches?

Why not use Sage Cell/Cloud?

Several reasons:

  • I don't have complete control of the UI, so I can't actually embed a Sage Cell.
  • The CAS tool is part of a much larger system with other services sharing resources.
  • Both are a bit overkill for the job since they include a UI and webserver - I just need to send the command and read the response.
2014-05-13 05:20:48 -0500 received badge  Supporter (source)