text:
_BUFFER := "";
OutputStreamZmqType := NewType(
    StreamsFamily,
    IsOutputTextStream and IsOutputStreamZmqRep );

InstallMethod( OutputStreamZmq,
    "output stream to Jupyter ZeroMQ",
    [ IsObject, IsZmqSocket, IsString ],
function(kernel, socket, streamname)
    # TODO: more specific, check kernel, connected socket, etc
    if not IsZmqSocket(socket)  then
        Error( "<socket> must be a IsZmqSocket" );
    fi;
    return Objectify( OutputStreamZmqType
                    , rec( kernel := kernel, socket := socket
                         , format := false, streamname := streamname ) );
end);

InstallMethod( OutputStreamZmq,
    "output stream to Jupyter ZeroMQ",
    [ IsObject, IsZmqSocket ],
    { kernel, socket } -> OutputStreamZmq(kernel, socket, "stdout" ) );

InstallMethod( ViewString,
    "output stream to Jupyter ZeroMQ",
    [ IsOutputStreamZmqRep ],
function( obj )
    # TODO: print some useful info about kernel/socket?
    return "OutputStreamZmq()";
end );

InstallMethod( WriteAll,
    "output text string",
    [ IsOutputTextStream and IsOutputStreamZmqRep,
      IsString ],
function( stream, string )
    local curmsg, msg;
    if IsBound(stream!.kernel!.CurrentMsg) then
        curmsg := stream!.kernel!.CurrentMsg;
    else
        curmsg := rec();
    fi;
    Append( _BUFFER, string );
    JupyterMsgSend( stream!.kernel
                  , stream!.kernel!.IOPub
                  , JupyterMsg( stream!.kernel
                              , "stream"
                              , curmsg
                              , rec( name := stream!.streamname
                                   , text := string )
                              , rec () ) );
    return true;
end );

InstallMethod( WriteByte,
    "output text string",
    [ IsOutputTextStream and IsOutputStreamZmqRep,
      IsInt ],
function(stream, byte)
    local curmsg, msg;
    if byte < 0 or 255 < byte  then
        Error( "<byte> must an integer between 0 and 255" );
    fi;
    # TODO
    if IsBound(stream!.kernel!.CurrentMsg) then
        curmsg := stream!.kernel!.CurrentMsg;
    else
        curmsg := rec();
    fi;
    Add( _BUFFER, CharInt(byte) );
    JupyterMsgSend( stream!.kernel
                  , stream!.kernel!.IOPub
                  , JupyterMsg( stream!.kernel
                              , "stream"
                              , curmsg
                              , rec( name := stream!.streamname
                                   , text := CharInt(byte) )
                              , rec () ) );

    return true;
end );

InstallMethod( PrintFormattingStatus, "output text string"
             , [ IsOutputTextStream and IsOutputStreamZmqRep ]
             , str -> str!.format);

InstallMethod( SetPrintFormattingStatus, "output text string"
             , [ IsOutputTextStream and IsOutputStreamZmqRep,
                 IsBool ],
function(str, stat)
    if stat = fail then
        Error("Print formatting status must be true or false");
    else
        str!.format := stat;
    fi;
end);

ext: gi

max_stars_repo_name: ZachNewbery/JupyterKernel

max_issues_repo_name: ZachNewbery/JupyterKernel

max_forks_repo_name: ZachNewbery/JupyterKernel


text:
# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

Class(IdFunc, DiagFunc, rec(
    range := self >> TInt
));

#F fConst(<N>, <c>) - constant diagonal function, <N> values of <c>
#F
Class(idConst, IdFunc, rec(
#    def := (N, c) -> Checked(IsPosInt0(N), rec(size:=N)),
    def := (N, c) -> rec(size:=N),
    inline := true,
    lambda := self >> let(i:=Ind(self.size), Lambda(i, self.params[2])),
    isReal := self >> IsRealNumber(self.params[2]),
    domain := self >> self.params[1],
));

Class(idId, IdFunc, rec(
    def := size -> rec(size := size),
    lambda := self >> let(i := Ind(self.size), Lambda(i,i)),
    #inverse := i->i,
    transpose := self >> self,
    domain := self >> self.params[1],
));

#NOTE Derive the idTensor from diagTensor
Class(idTensor, fTensorBase, rec(
    print := FuncClassOper.print,
#    range := self >> UnifyTypes(List(self.children(), x->x.range())),
    combine_op := (self, jv, split, f, g) >> f.relaxed_at(jv, split[1]) * g.relaxed_at(jv, split[2]),

    updateRange := self >> UnifyTypes(List(self.children(), x->x.range())),
    range       := self >> self.updateRange(),
#    combine_op := (self, split, F, G) >> F.at(split[1]) * G.at(split[2])
    # idTensor is something else again ???
#    combine_op := (self, split, F, G) >> self.child(2).domain() * F.at(split[1]) + G.at(split[2])
));

#Class(idfTensor, fTensorBase, rec(
#    print := FuncClassOper.print,
#    updateRange := self >> UnifyTypes(List(self.children(), x->x.range())),
#    combine_op := (self, split, F, G) >> self.child(2).domain() * F.at(split[1]) + G.at(split[2])
#));
#

Class(idCompose, fCompose);

Class(Id, Diag, rec(
  toAMat := self >> IdentityMatAMat(self.element.domain()),
  domain := self >> self.element.domain()
));


text:
# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

NEON.setRules(rec(
  binrules := [ rec(
          perm := rec(
              spl := L(8, 2),
              l := 1,
              N := 8,
              n := 2,
              r := 1 ),
          instr := [ rec(
                  instr := vpacklo_neon,
                  p := [  ] ), rec(
                  instr := vpackhi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VPerm(L(8, 2), (y, x) -> chain(assign(vref(y, 0, 4), vpacklo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vpackhi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) ), rec(
          perm := rec(
              spl := L(8, 4),
              l := 1,
              N := 8,
              n := 4,
              r := 1 ),
          instr := [ rec(
                  instr := vunpacklo_neon,
                  p := [  ] ), rec(
                  instr := vunpackhi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VPerm(L(8, 4), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) ), rec(
          perm := rec(
              spl := Tensor(
                  L(4, 2),
                  I(2)
                ),
              l := 1,
              N := 4,
              n := 2,
              r := 2 ),
          instr := [ rec(
                  instr := vunpacklolo2_neon,
                  p := [  ] ), rec(
                  instr := vunpackhihi2_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VPerm(Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklolo2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhihi2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) ), rec(
          perm := rec(
              spl := Tensor(
                  I(2),
                  L(4, 2)
                ),
              l := 2,
              N := 4,
              n := 2,
              r := 1 ),
          instr := [ rec(
                  instr := vpacklo_neon,
                  p := [  ] ), rec(
                  instr := vpackhi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VPerm(Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklolo2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhihi2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) *
            VPerm(Tensor(
                L(4, 2),
                I(2)
              ) *
              Tensor(
                I(2),
                L(4, 2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vpacklo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vpackhi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 4.2000000000000002) *
            VTensor(I(2), 4) ), rec(
          perm := rec(
              spl := L(8, 4),
              l := 1,
              N := 8,
              n := 4,
              r := 1 ),
          instr := [ rec(
                  instr := vtransposelo_neon,
                  p := [  ] ), rec(
                  instr := vtransposehi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VPerm(Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklolo2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhihi2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) *
            VPerm(Tensor(
                L(4, 2),
                I(2)
              ) *
              L(8, 4), (y, x) -> chain(assign(vref(y, 0, 4), vtransposelo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vtransposehi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 4.2000000000000002) *
            VTensor(I(2), 4) ), rec(
          perm := rec(
              spl := Tensor(
                  I(2),
                  L(4, 2)
                ),
              l := 2,
              N := 4,
              n := 2,
              r := 1 ),
          instr := [ rec(
                  instr := vunpacklo_neon,
                  p := [  ] ), rec(
                  instr := vunpackhi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VTensor(I(2), 4) *
            VPerm(Tensor(
                I(2),
                L(4, 2)
              ) *
              Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 4.2000000000000002) *
            VPerm(Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklolo2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhihi2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) ), rec(
          perm := rec(
              spl := L(8, 2),
              l := 1,
              N := 8,
              n := 2,
              r := 1 ),
          instr := [ rec(
                  instr := vtransposelo_neon,
                  p := [  ] ), rec(
                  instr := vtransposehi_neon,
                  p := [  ] ) ],
          v := 4,
          vperm := VTensor(I(2), 4) *
            VPerm(L(8, 2) *
              Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vtransposelo_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vtransposehi_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 4.2000000000000002) *
            VPerm(Tensor(
                L(4, 2),
                I(2)
              ), (y, x) -> chain(assign(vref(y, 0, 4), vunpacklolo2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ])), assign(vref(y, 4, 4), vunpackhihi2_neon(vref(x, 0, 4), vref(x, 4, 4), [  ]))), 4, 2) ) ],
  unrules := [  ],
  x_I_vby2 := [ [ rec(
              instr := vunpacklolo2_neon,
              p := [  ] ), rec(
              instr := vunpacklohi2_neon,
              p := [  ] ) ], [ rec(
              instr := vunpackhilo2_neon,
              p := [  ] ), rec(
              instr := vunpackhihi2_neon,
              p := [  ] ) ] ] ));
NEON_HALF.setRules(rec(
  binrules := [ rec(
          perm := rec(
              spl := L(4, 2),
              l := 1,
              N := 4,
              n := 2,
              r := 1 ),
          instr := [ rec(
                  instr := vunpacklo_half,
                  p := [  ] ), rec(
                  instr := vunpackhi_half,
                  p := [  ] ) ],
          v := 2,
          vperm := VPerm(L(4, 2), (y, x) -> chain(assign(vref(y, 0, 2), vunpacklo_half(vref(x, 0, 2), vref(x, 2, 2), [  ])), assign(vref(y, 2, 2), vunpackhi_half(vref(x, 0, 2), vref(x, 2, 2), [  ]))), 2, 2) ) ],
  unrules := [  ],
  x_I_vby2 := [ [ rec(
              instr := vunpacklo_half,
              p := [  ] ), [  ] ], [ [  ], rec(
              instr := vunpackhi_half,
              p := [  ] ) ] ] ));