Ask Your Question

manage my functions in SAGE

asked 2013-11-11 03:44:45 +0200

gundamlh gravatar image

updated 2013-11-11 07:00:58 +0200

tmonteil gravatar image

I prefer bash/console as my SAGE editor with both hands always on the keyboard.

However, I want to manage those functions that are defined by myself in a way as in MATLAB, explicitly store m-functions in a toolbox folder and load them using addpath/import if necessary. Moreover, it seems inconvenient to modify/extend a function in bash/console in SAGE.

EMACS is perhaps a good choice?

Thanks in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-11-11 06:53:06 +0200

tmonteil gravatar image

updated 2013-11-11 07:06:46 +0200

I you are already used to some particular editor for coding, you can/should keep it, so that you will be at ease with it (shortcuts, syntaxic coloration,...). What is worth noticing, is that there are tools to ease a "store things in files and use them in a Sage console" workflow, whatever your editor is.

If you want to store some functions in a file and use them in a Sage shell you can do:

sage: %runfile('/path/to/file.sage')

and the file will be run once, or

sage: attach('/path/to/file.sage')

and the file will be re-run each time you save it (useful when you want to test its parts while modifying it).

If you name the file instead of file.sage, then there will not be any Sage preparsing (e.g. ^ will not mean power). Note that only .py files are accepted in the Sage source code, so you should use them if you plan to insert your code as a part of Sage.

Now, if you want to import custom functions/classes/variables as modules with the import command, you can store your files in some my_directory, and tell Sage that this directory contains interesting things for it as follows:

import sys

Then you can do:

from my_file import my_function

If you want to automatically append my_directory at Sage startup, you can add the previous lines in the file $HOME/.sage/init.sage

edit flag offensive delete link more


Really appreciate your detailed explanation!

gundamlh gravatar imagegundamlh ( 2013-11-11 11:36:13 +0200 )edit

I am an amateur in Linux, bash commands, emacs. Additionally, I only know EMACS can be used to edit SAGE-files, besides notebook of SAGE and bash/console.

gundamlh gravatar imagegundamlh ( 2013-11-11 12:38:08 +0200 )edit

I can run .sage file as a stand-alone file like MATLAB-script, but i cannot call it directly with input arguments as MATLAB function/method. DO I HAVE TO write the method in Python?

gundamlh gravatar imagegundamlh ( 2013-11-28 14:25:22 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-11-11 03:44:45 +0200

Seen: 840 times

Last updated: Nov 11 '13