Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Support and Testing.
Download

Tutorial for using %julia in Sage Worksheets

1604 views
�
��QTc@sdZddlZddlZddlZddlZddlmZd�ZddlmZm	Z	m
Z
mZmZddl
mZdZdefd	��YZd
e	fd��YZdefd
��YZde
fd��YZd�Ze�Zd�ZddlZd�ZdS(sd
Pexpect-based interface to Julia

EXAMPLES::

    TODO

AUTHORS:
    -- William Stein (2014-10-26)
i����N(tuuid4cCs
tt��S(N(tstrR(((s
./julia.pytuuids(tExpectt
ExpectElementtExpectFunctiontFunctionElementtgc_disabled(tRingElementitJuliacBs�eZdddddd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zddd�ZRS(i��cCskd|_tj|ddd|jddd|d|d	|d
|dtdtd
|�
d|_d|_dS(s2
        Pexpect-based interface to Julia
        sjulia>tnameR	tprompttcommandtjuliatmaxreadtservert
server_tmpdirtscript_subdirectorytrestart_on_ctrlct
verbose_starttlogfileiiN(t_promptRt__init__tFalset_Julia__seqt_Julia__in_seq(tselfRRRRR((s
./julia.pyR#s				cCs]ttj�}d|d<tj|jd|jd|�|_d|j_|jj	d�dS(s	
        tvt100tTERMRtenvis\[0Kjulia>N(
tdicttostenvirontpexpecttspawnt_Expect__commandt_Expect__logfilet_expecttdelaybeforesendtexpect(Rtpexpect_env((s
./julia.pyt_start?s

$cKswt|t�r!|jd�}nd}d}|jsC|j�nt��%|j}t�}|d|}|j|�|j|�|j	}||_
|j|�|j
|j	7_
|j|�|j
|j	7_
|j|�}	|	dkr�|S|t|�|	}|j
|�}	|	dkr)|S||	 j�}|jd�ri|jdd�}
t|
��n|SWd	QXd	S(
s	
        tutf8s[?2004lsjulia> s,




__ans__=ans;println("%s");ans=__ans__;
i����sERROR:sin anonymous at no filetN(t
isinstancetunicodetencodeR%R)RRtsendR'tbeforet_last_resulttrfindtlentfindtrstript
startswithtreplacetRuntimeError(RtcodetignoredtSTARTtENDtstutlinetresulttitjulia_error((s
./julia.pytevalHs<	

		

		

cCs
|d�S(sV
        EXAMPLES::

            sage: julia._an_element_impl()
            0
        i((R((s
./julia.pyt_an_element_implkscCsHd||f}|j|�}d|krDtd||f��ndS(s)
        Set the variable var to the given value.

        EXAMPLES::

            sage: julia.set('x', '2')
            sage: julia.get('x')
            '2'

        TEST:

        It must also be possible to eval the variable by name::

            sage: julia.eval('x')
            '2'
        s%s=%s;s***s6Error executing code in Sage
CODE:
	%s
SAGE ERROR:
	%sN(RCt	TypeError(Rtvartvaluetcmdtout((s
./julia.pytsettscCs|j|�}|S(st
        EXAMPLES::

            sage: julia.set('x', '2')
            sage: julia.get('x')
            '2'
        (RC(RRFRI((s
./julia.pytget�scCsdS(NsJulia Interpreter((R((s
./julia.pyt_repr_�scCsttg�fS(sB
        EXAMPLES::

            sage: julia.__reduce__()
        (treduce_load_Juliattuple(R((s
./julia.pyt
__reduce__�scCstS(s�
        EXAMPLES::

            sage: julia._function_class()
            <class 'sage.interfaces.julia.JuliaFunction'>
         (t
JuliaFunction(R((s
./julia.pyt_function_class�scCsdS(s�
        EXAMPLES::

            sage: julia._quit_string()
            'quit()'

            sage: l = Julia()
            sage: l._start()
            sage: l.quit()
            sage: l.is_running()
            False
        squit()((R((s
./julia.pyt_quit_string�s
cCsdS(sb
        EXAMPLES::

            sage: julia._read_in_file_command(tmp_filename()) # TODO
        N((Rtfilename((s
./julia.pyt_read_in_file_command�scCs�tjd�}g}xo|jd�d D]Z}|jd�}|dkr)||dj�}|r�|j�r�|j|�q�q)q)W|j�|S(sc
        EXAMPLES::

            sage: julia.trait_names()
            ['ANY', ..., 'zip']
        s		s����tGi(R
RCtsplitR4tstriptisalnumtappendtsort(RR=tvtxRAtc((s
./julia.pyttrait_names�s
cCs
t�dS(s�
        EXAMPLES::

            sage: julia.kill('x')
            Traceback (most recent call last):
            ...
            NotImplementedError
        N(tNotImplementedError(RRF((s
./julia.pytkill�s	cCst�dS(s�
        Spawn a new Julia command-line session.

        EXAMPLES::

            sage: julia.console() #not tested
            ...
        N(t
julia_console(R((s
./julia.pytconsole�s	cCs
|jd�S(s�
        Returns the version of Julia being used.

        EXAMPLES::

            sage: julia.version()
            'Version information is given by julia.console().'
        s
versioninfo()(RC(R((s
./julia.pytversion�s	cCstS(s~
        EXAMPLES::

            sage: julia._object_class()
            <class 'sage.interfaces.julia.JuliaElement'>
        (tJuliaElement(R((s
./julia.pyt
_object_class�scCstS(s�
        EXAMPLES::

            sage: julia._function_class()
            <class 'sage.interfaces.julia.JuliaFunction'>
        (RP(R((s
./julia.pyRQ�scCstS(s�
        EXAMPLES::

            sage: julia._function_element_class()
            <class 'sage.interfaces.julia.JuliaFunctionElement'>
        (tJuliaFunctionElement(R((s
./julia.pyt_function_element_classscCsdS(sW
        EXAMPLES::

            sage: julia._true_symbol()
            'true'
        ttrue((R((s
./julia.pyt_true_symbolscCsdS(sY
        EXAMPLES::

            sage: julia._false_symbol()
            'false'
        tfalse((R((s
./julia.pyt
_false_symbolscCsdS(s	
        s==((R((s
./julia.pyt_equality_symbol scCs,d|krtd��n|jd|�S(s
        EXAMPLES::


        t"squote in command names
help("%s")(t
ValueErrorRC(RR((s
./julia.pythelp%scCs^|j||�\}}|j|�|jd|djg|D]}|j�^q>�f�S(s�
        EXAMPLES::

            sage: julia.function_call('sin', ['2'])
            0.9092974
            sage: julia.sin(2)
            0.9092974
        s%s(%s)t,(t_convert_args_kwdst_check_valid_function_nametnewtjoinR
(RtfunctiontargstkwdsR=((s
./julia.pyt
function_call/s	
N(t__name__t
__module__tNoneRR)RCRDRJRKRLRORQRRRTR^R`RbRcReRgRiRkRlRoRx(((s
./julia.pyR	"s6			#																										
RdcBsPeZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z	RS(cCs|j�j�S(N(t_check_validR^(R((s
./julia.pyR^=scCs�|j�}t|d�s.||j�k	r=||�}n|jd|j�|j�f�|j�krrdS|jd|j�|j�f�|j�kr�dSdSdS(s]
        EXAMPLES::

            sage: one = julia(1); two = julia(2)
            sage: one == one
            True
            sage: one != two
            True
            sage: one < two
            True
            sage: two > one
            True
            sage: one < 1
            False
            sage: two == 2
            True

        tparents%s == %sis%s < %si����iN(R|thasattrR}RCR
Ri(RtothertP((s
./julia.pyt__cmp__As"11cCs/|j�}|jd|j��|j�kS(s�
        EXAMPLES::

            sage: julia(2).bool()
            True
            sage: julia(0).bool()
            False
            sage: bool(julia(2))
            True
        sbool(%s)(R|RCR
Ri(RR�((s
./julia.pytbool_scCs)|j�}|jd|j|jf�S(sp
        EXAMPLES::

            sage: a = julia(1); b = julia(2)
            sage: a + b
            3
        s%s + %s(R|Rst_name(RtrightR�((s
./julia.pyt_add_mscCs)|j�}|jd|j|jf�S(sq
        EXAMPLES::

            sage: a = julia(1); b = julia(2)
            sage: a - b
            -1
        s%s - %s(R|RsR�(RR�R�((s
./julia.pyt_sub_xscCs)|j�}|jd|j|jf�S(sp
        EXAMPLES::

            sage: a = julia(1); b = julia(2)
            sage: a * b
            2
        s%s * %s(R|RsR�(RR�R�((s
./julia.pyt_mul_�scCs)|j�}|jd|j|jf�S(sr
        EXAMPLES::

            sage: a = julia(1); b = julia(2)
            sage: a / b
            1/2
        s%s / %s(R|RsR�(RR�R�((s
./julia.pyt_div_�scCs5|j�}||�}|jd|j|jf�S(sa
        EXAMPLES::

            sage: a = julia(3)
            sage: a^3
            27
        s%s ^ %s(R|RsR�(RtnR�R�((s
./julia.pyt__pow__�s(
RyRzR^R�R�R�R�R�R�R�(((s
./julia.pyRd<s							RfcBseZd�ZRS(cCs|jj�}|j|j�S(s�
        EXAMPLES::

            sage: two = julia(2)
            sage: two.sin._sage_doc_()
            'Base.sin(x)

   Compute sine of "x", where "x" is in radians'
        (t_objR}RoR�(RtM((s
./julia.pyt
_sage_doc_�s(RyRzR�(((s
./julia.pyRf�sRPcBseZd�ZRS(cCs|j}|j|j�S(s�
        EXAMPLES::

            sage: julia.sin._sage_doc_()
            Traceback (most recent call last):
            ...
            NotImplementedError
        (t_parentRoR�(RR�((s
./julia.pyR��s		(RyRzR�(((s
./julia.pyRP�scCs
t|t�S(s�
    EXAMPLES::

        sage: from sage.interfaces.julia import is_JuliaElement
        sage: is_JuliaElement(julia(2))
        True
        sage: is_JuliaElement(2)
        False
    (R,Rd(R\((s
./julia.pytis_JuliaElement�s
cCstS(s�
    EXAMPLES::

        sage: from sage.interfaces.julia import reduce_load_Julia
        sage: reduce_load_Julia()
        Julia Interpreter
    (R
(((s
./julia.pyRM�scCstjd�dS(sx
    Spawn a new Julia command-line session.

    EXAMPLES::

        sage: julia.console() #not tested
        ...
    R
N(Rtsystem(((s
./julia.pyRa�s	(t__doc__RR!trandomtstringRRtsage.interfaces.expectRRRRRtsage.structure.elementRt
PROMPT_LENGTHR	RdRfRPR�R
RMRa(((s
./julia.pyt<module>
s 0	(�i