|
|
|
last edited 1 year ago by test1 |
| 1 2 3 | ||
|
Editor: page
Time: 2007/09/19 18:50:23 GMT-7 |
||
| Note: | ||
changed: - Adding new functions to PrimitiveArray in an attempt to improve performance. new(n,x:NNI->S) -- create new array of size n with values from x(i),i=0..n-1 fill!(n,x:NNI->S) -- set the elements of array with values from x(i),i=0..n-1 This is the code for Axiom's PrimitiveArray domain with two small changes for options to call 'new' and 'fill!' with an initialization function rather than just a constant. \begin{spad} )abbrev domain XPRIMARR XPrimitiveArray NNI ==> NonNegativeInteger -- This provides a fast array type with no bound checking on elt's. -- Minimum index is 0 in this type, cannot be changed XPrimitiveArray(S:Type): OneDimensionalArrayAggregate S with new:(NNI,NNI->S)->% fill_!:(%,NNI->S)->% == add Qmax ==> QVMAXINDEX$Lisp Qsize ==> QVSIZE$Lisp Qelt ==> ELT$Lisp Qsetelt ==> SETELT$Lisp Qnew ==> GETREFV$Lisp #x == Qsize x minIndex x == 0 empty() == Qnew(0$Lisp) new(n:NNI, x:S):% == fill_!(Qnew n, x) new(n:NNI, x:NNI->S):% == fill_!(Qnew n, x) qelt(x, i) == Qelt(x, i) elt(x:%, i:Integer) == Qelt(x, i) qsetelt_!(x, i, s) == Qsetelt(x, i, s) setelt(x:%, i:Integer, s:S) == Qsetelt(x, i, s) fill_!(x:%, s:S):% == (for i in 0..Qmax x repeat Qsetelt(x, i, s); x) fill_!(x:%, s:NNI->S):% == (for i in 0..Qmax x repeat Qsetelt(x, i, s(i)); x) \end{spad} How long does this take? First, it seems to make a big difference whether the initialization is done by an anonymous function or by a named function since the latter can be compiled and called with very little overhead. In other words using:: init1(x:Integer):Integer == random(100)-random(100) is much better than this: \begin{axiom} )time on l0:PRIMARR(INT):=new(100000,0); map!(x+->random(100)-random(100),l0); \end{axiom} Old way using a compiled initialization function with the 'map!' function: \begin{axiom} init1(x:Integer):Integer == random(100)-random(100) l1:PRIMARR(INT):=new(100000,0); map!(init1,l1); \end{axiom} New way with the call to the initialization function builtin to the 'new' function: \begin{axiom} init2(x:NNI):Integer == random(100)-random(100) l2:XPRIMARR(INT):=new(100000,init2); \end{axiom} Show the first few entries \begin{axiom} for i in 0..10 repeat output [l0.i,l1.i,l2.i] \end{axiom}
Adding new functions to PrimitiveArray? in an attempt to improve performance.
This is the code for Axiom's PrimitiveArray? domain with two small
changes for options to call new and fill! with an initialization
function rather than just a constant.
(1) -> <spad>
)abbrev domain XPRIMARR XPrimitiveArray NNI ==> NonNegativeInteger -- This provides a fast array type with no bound checking on elt's. -- Minimum index is 0 in this type,cannot be changed XPrimitiveArray(S:Type): OneDimensionalArrayAggregate S with new:(NNI, NNI->S)->% fill_!:(%, NNI->S)->% == add Qmax ==> QVMAXINDEX$Lisp Qsize ==> QVSIZE$Lisp Qelt ==> ELT$Lisp Qsetelt ==> SETELT$Lisp Qnew ==> GETREFV$Lisp
#x == Qsize x minIndex x == 0 empty() == Qnew(0$Lisp) new(n:NNI,x:S):% == fill_!(Qnew n, x) new(n:NNI, x:NNI->S):% == fill_!(Qnew n, x) qelt(x, i) == Qelt(x, i) elt(x:%, i:Integer) == Qelt(x, i) qsetelt_!(x, i, s) == Qsetelt(x, i, s) setelt(x:%, i:Integer, s:S) == Qsetelt(x, i, s) fill_!(x:%, s:S):% == (for i in 0..Qmax x repeat Qsetelt(x, i, s); x) fill_!(x:%, s:NNI->S):% == (for i in 0..Qmax x repeat Qsetelt(x, i, s(i)); x)</spad>
Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/7193926653459221605-25px001.spad
using old system compiler.
XPRIMARR abbreviates domain XPrimitiveArray
------------------------------------------------------------------------
initializing NRLIB XPRIMARR for XPrimitiveArray
compiling into NRLIB XPRIMARR
processing macro definition Qmax ==> Sel(Lisp, QVMAXINDEX)
processing macro definition Qsize ==> Sel(Lisp, QVSIZE)
processing macro definition Qelt ==> Sel(Lisp, ELT)
processing macro definition Qsetelt ==> Sel(Lisp, SETELT)
processing macro definition Qnew ==> Sel(Lisp, GETREFV)
compiling exported # : % -> NonNegativeInteger
XPRIMARR;#;%Nni;1 is replaced by QVSIZE
Time: 0 SEC.
compiling exported minIndex : % -> Integer
XPRIMARR;minIndex;%I;2 is replaced by 0
Time: 0 SEC.
compiling exported empty : () -> %
XPRIMARR;empty;%;3 is replaced by GETREFV0
Time: 0 SEC.
compiling exported new : (NonNegativeInteger, S) -> %
****** comp fails at level 4 with expression: ******
error in function new
(|fill_!| ((|Sel| |Lisp| GETREFV) |n|) |x|)
****** level 4 ******
x:= x
m:= %
f:=
((((#:G4 #) (|x| # . #1=#) (|n| # #) (|x| . #1#) ...)))
>> Apparent user error:
Cannot coerce x
of mode S
to modeHow long does this take?
First, it seems to make a big difference whether the initialization is done by an anonymous function or by a named function since the latter can be compiled and called with very little overhead. In other words using:
init1(x:Integer):Integer == random(100)-random(100)
is much better than this:
)time on
l0:PRIMARR(INT):=new(100000,0); map!(x+->random(100)-random(100), l0);
Time: 0 sec
Old way using a compiled initialization function with the map!
function:
init1(x:Integer):Integer == random(100)-random(100)
Function declaration init1 : Integer -> Integer has been added to workspace.
Time: 0 sec l1:PRIMARR(INT):=new(100000,0); map!(init1, l1);
Compiling function init1 with type Integer -> Integer
Time: 0 sec
New way with the call to the initialization function builtin to
the new function:
init2(x:NNI):Integer == random(100)-random(100)
Function declaration init2 : NonNegativeInteger -> Integer has been added to workspace.
Time: 0 sec l2:XPRIMARR(INT):=new(100000,init2);
XPrimitiveArray is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead?
Show the first few entries
for i in 0..10 repeat output [l0.i,l1.i, l2.i]
There are no library operations named l2 Use HyperDoc Browse or issue )what op l2 to learn if there is any operation containing " l2 " in its name. Cannot find a definition or applicable library operation named l2 with argument type(s) NonNegativeInteger
Perhaps you should use "@" to indicate the required return type,or "$" to specify which version of the function you need. FriCAS will attempt to step through and interpret the code. There are no library operations named l2 Use HyperDoc Browse or issue )what op l2 to learn if there is any operation containing " l2 " in its name.
Cannot find a definition or applicable library operation named l2 with argument type(s) NonNegativeInteger
Perhaps you should use "@" to indicate the required return type,or "$" to specify which version of the function you need.