> FrAid
 

Embedding FrAid

Introduction

Qutie a few languages nowadays allow Java to be used in an interactive scripting environment. These include Groovy, BeanShell, Jython to name just a few. Here is how you can use FrAid from such an environment (the examples are tested with Groovy but they should run on any of the listed above languages or Java itself).

Note
Some of the mentioned scripting environments use custom class loaders which have problem loading FrAid if the installation contains more than one jar file. If you get any such errors use the single jar FrAid installation.

Uses

Comment:Plot a function:Execute a function:FrAid script required:
Execute it as a FrAid script
                        import org.fraid.Scripting

                        s = new Scripting()
                        s.exec("plot(sin);")
                    
Result:sin
print new org.fraid.Scripting().exec("sin(2);")
                    
Result: (0.9092974268256817 - 0.0i)
Yes
Pass a subclass of ComplexFunction
                        import org.fraid.graphics.functions.*
                        import org.fraid.complex.functions.*

                        new plot().exec(sin)
                    
Result:sin

                    
Result:
No
Pass a instance of a subclass of ComplexFunction
                        import org.fraid.graphics.functions.*
                        import org.fraid.complex.functions.*

                        new plot().exec(new sin())
                    
Result:sin
                        import org.fraid.complex.functions.*
                        print new sin().exec(2)
                    
Result: (0.9092974268256817 - 0.0i)
No
Get it from the symbol table
                        import org.fraid.Scripting
                        import org.fraid.graphics.functions.*
                        import org.fraid.complex.functions.*

                        s = new Scripting()
                        new plot().exec(s.get_f("sin"))
                    
Result:sin
                        print org.fraid.Scripting.get_f("sin").exec(2) 
                    
Result: (0.9092974268256817 - 0.0i)
No
Define your own function
                        import org.fraid.Scripting
                        import org.fraid.graphics.functions.*
                        import org.fraid.complex.functions.*

                        s = new Scripting()
                        my_f = s.def_f("f(x)=sin(x)^cos(x);")
                        new plot().exec(sin, my_f)
                    
Result:sin
                        print new org.fraid.Scripting().def_f("f(x)=sin(x)^cos(x);").exec(2)
                    
Result: (1.0403617660940427 + 0.0i)
Yes
Define and get your own function from the symbol table
                        import org.fraid.Scripting
                        import org.fraid.graphics.functions.*
                        import org.fraid.complex.functions.*

                        s = new Scripting()
                        s.def_f("f(x)=sin(x)^cos(x);")
                        new plot().exec(sin, s.get_f("f"))
                    
Result:sin
                        s = new org.fraid.Scripting()
                        s.def_f("f(x)=sin(x)^cos(x);")
                        print s.get_f("f").exec(2)
                    
Result: (1.0403617660940427 + 0.0i)
Yes
Define your own function within the scripting environment
                        import org.fraid.function.*
                        import org.fraid.complex.functions.*
                        import org.fraid.graphics.functions.*
                        import org.netlib.math.complex.Complex

                        class MyFun extends ComplexFunction{
                        MyFun(){ super(1); }
                        Complex invoke( Complex[] args ){
                        return args[0].sin().pow(args[0].cos());
                        }
                        }
                        new plot().exec( sin, new MyFun() )
                    
Result:sin
                        import org.fraid.function.*
                        import org.fraid.complex.functions.*
                        import org.netlib.math.complex.Complex
                        class MyFun extends ComplexFunction{
                        MyFun(){ super(1); }
                        Complex invoke( Complex[] args ){
                        return args[0].sin().pow(args[0].cos());
                        }
                        }
                        print new MyFun().exec(2)
                    
Result: (1.0403617660940427 + 0.0i)
No

Other useful functions in org.fraid.Scripting and org.fraid.ScriptingGraphics

DescriptionScriptFrAid script required
Query the symbol table for all functions whith names starting wiht "si".
                        print org.fraid.Scripting.query_f("si")
                    
Result:[sinh:1, sin:1, sign:1]
No
Query the symbol table, select a function from the result and invoke it.
                        s = new org.fraid.Scripting()
                        q=s.query_f("si")
                        for( k in q ){
                          if( k == "sin" ){
                            print s.get_f( k ).
                            exec(2)
                          }
                        }                        
                    
Result: (0.9092974268256817 - 0.0i)
No
Plot some graphics and then close all the windows.
                            s = new org.fraid.Scripting()
                            s.exec("plot(sin);\
                            f(x,r)=r*x*(1-x);\
                            orbit(f);")
                            print org.fraid.ScriptingGraphics.close_w( org.fraid.ScriptingGraphics.query_w() )
                    
Result: 2 //number of windows closed
Yes
- Open a window;
- Plot something in it;
- Change the color, span and size of the window;
- Repaint.
                            import org.fraid.complex.functions.*
                            import org.fraid.graphics.functions.*
                            s = new org.fraid.Scripting()
                            my_win = org.fraid.ScriptingGraphics.new_w()
                            new plot().exec( my_win, sin )
                            p = s.get_p(my_win)
                            p.backgroundColor = new java.awt.Color(100,0,100)
                            p.upperLeft = new org.fraid.math.DoublePoint( -10, 10 )
                            p.bottomRight = new org.fraid.math.DoublePoint( 10, -10 )
                            org.fraid.ScriptingGraphics.size_w(my_win,300,300)
                            org.fraid.ScriptingGraphics.paint_w(my_win)                               
                    
Result:
No
- Open a FrAid Console and work with the interpreter directly.
                        prog = "plot(sin);"
                        org.fraid.ScriptingConsole.console( prog )  //prog can be empty ("")
                    
Result:
Yes