Jump to content



Photo

Cplua Project: LNA


  • This topic is locked This topic is locked
187 replies to this topic

#1 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 19 September 2005 - 09:51 PM

LNA (aka LuaNumAn) stands for "Lua Numerical Analysis". It is a library of Numerical Analysis functions written in Lua.

This project is developed in CPLua. If you don't know what "Numerical Analysis" is, you will not find something useful here. If you are interested, but you don't know what "CPLua" is, run, dont walk, to the CPLua topic to get an idea on what this programming language can do.

LNA includes several functions implementing rather complex numerical methods; utility and plotting functions are also included in the package. I'm planning to add new functions in the library only if I have the time to write the code in a fully modularized way, to make the functions as flexible as it gets, and only after extensive testing. This means that the project will evolve rather slowly, but hopefully in a regular basis.

To be honest, I don't expect too much forumers to be interested. Nevertheless, I already know that someone will find this project useful. If you have any comments, or if you want for a particular numerical method to be implemented in the library, you are welcome to post here. If you are looking for games, you will disappointed. LNA is definitely not for you, and you should look elsewhere; don't say that you haven't been warned ;).

LNA comes with full documentation. If you cannot solve a particular problem, I will be happy to help, but please read the documentation first. Many questions, such as "how can I call the function xxx from my Lua program?", or "what the argument yyy of the function xxx does?" are extensively discussed in the "LNA programmer's guide". Several examples are also included.

Latest version of LNA is 1.60. There is a link in the File Sharing, where you will find the latest version and the corresponding documentation.

#2 unique33

unique33

    Casio Freak

  • Possibly hacked
  • PipPipPipPip
  • 229 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    classpad 300 , fx5500

Posted 19 September 2005 - 11:07 PM

When I integrate the third example in Drumberg within (0,3) the output screen prints ***************
I think I do something wrong ! ;)
please explain about it ,thanks .

#3 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 19 September 2005 - 11:18 PM

When I integrate the third example in Drumberg within (0,3) the output screen prints ***************
I think I do something wrong ! ;)
please explain about it ,thanks .

Yes, you are doing something very wrong: you are dividing by zero! Look at the definition of the third function: it is f3(x) = x sin(1/x) sqrt(|x-1|), and has a singularity at x=0. The function Romberg can only be used if the function to be integrated has no singularities at the integration interval (look the documentation for Romberg). That's why in this example the lowest limit is set to 1E-6, not to zero ;).
I'm planning to add in the library an integration method able to handle integrable singularities, but I think that this should have a low priority for the moment, given that several more important numerical methods are missing from this first version.

#4 unique33

unique33

    Casio Freak

  • Possibly hacked
  • PipPipPipPip
  • 229 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    classpad 300 , fx5500

Posted 21 September 2005 - 08:40 PM

I wanted to write a program for Numerical differentiation Centeral method , I studied some books but I did not find the proper sumation function for this method .

I think adding a Lua numerical differentiation would be usefull .

#5 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 21 September 2005 - 09:06 PM

I wanted to write a program for Numerical differentiation Centeral method , I studied some books but I did not find the proper sumation function for this method .
I think adding a Lua numerical differentiation would be usefull .

You are absolutely right, numerical differentiation is really useful. I assume that by "central method", you mean the central divided differences. However, this method is totally unsafe (in general, numerical differentiation is very dangerous). The only method that really works is differentiation by Cubic Splines; it gives very accurat results, provided that your function is rather smooth. Yesterday, I was thinking on what to add next in LuaNumAn. I decided to add a method for solving systems of ordinary differential equations (specifically, a Runge-Kutta method of 4th order with Richardson extrapolation and error control - a really robust method). After that, I'm planning to implement Cubic Splines with differentiation support (for 1st and 2nd derivatives). Be patient, you will see that implemented soon (at least, I hope so). However, the lack of matrix support in Lua really slows the implementation of such numerical methods.
If you have any suggestions, let me know.

#6 unique33

unique33

    Casio Freak

  • Possibly hacked
  • PipPipPipPip
  • 229 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    classpad 300 , fx5500

Posted 22 September 2005 - 08:28 PM

your Romberg program is so usefull .
I have a suggestion for it :
at the university , the teachers want all the outputs for any value of N from 2 upto 16
for Example
N=2
...
N=4
...
and so on

so If you add an other argument to your function to print the results in this order it would be so usefull for university students .
but maybe it is possible just for simple functions .
:(

thanks.

#7 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 22 September 2005 - 10:05 PM

your Romberg program is so usefull .

Thanks! Nice to know that it is useful to someone :).

I have a suggestion for it :
at the university , the teachers want all the outputs for any value of N from 2 upto 16
for Example
N=2
...
N=4
...
and so on
so If you add an other argument to your function to print the results in this order it would be so usefull for university students .
but maybe it is possible just for simple functions .
:(

You can already do this, by using the optional argument show :). However, due to Lua restrictions, concerning the optional arguments, you must also pass the arguments eps and k (show is the third optional argument, and Lua requires the first two optional arguments to be used, even if you don't need them). Be sure to keep k equal to 2, so that the trapezoidal rule is used in each step. For example, Romberg(f,a,b,1E-6,2,true) will print all the intermediate steps: the first step corresponds to N=1, the second to N=2, the third to N=4, the fourth to N=8, and so on. Actually, the third example in the driver program Dromberg does exactly this.

To be honest, I'm planning to add Numerical Analysis functions for solving "real" problems, not just for educational use. For that reason, I decided to include rather complex methods, and reject several simple, introductory, methods. Romberg integration is perharps one of the few "real-life" methods that are included in a University course.
If you are interested on other numerical methods for educational use, let me know. I can add them somehow, but not in the main LuaNumAn library.

#8 unique33

unique33

    Casio Freak

  • Possibly hacked
  • PipPipPipPip
  • 229 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    classpad 300 , fx5500

Posted 22 September 2005 - 10:28 PM

You can already do this, by using the optional argument show :). However, due to Lua restrictions, concerning the optional arguments, you must also pass the arguments eps and k (show is the third optional argument, and Lua requires the first two optional arguments to be used, even if you don't need them). Be sure to keep k equal to 2, so that the trapezoidal rule is used in each step. For example, Romberg(f,a,b,1E-6,2,true) will print all the intermediate steps: the first step corresponds to N=1, the second to N=2, the third to N=4, the fourth to N=8, and so on. Actually, the third example in the driver program Dromberg does exactly this.


thanks. for your explanations . :)

#9 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 22 September 2005 - 10:46 PM

Version 1.10 is out.

I have uploaded LuaNumAn version 1.10. Major changes:
(1) A function for solving first-order ordinary differential equation(s) has been added. It implements a Runge-Kutta method of fourth order with adaptive stepsize control via Richardson extrapolation. This method has a very good error control and it is highly accurate. The LuaNumAn function can solve a system of first-order ordinary differential equations of arbitrary size. Of course, systems of high-order differential equations can be solved as well, since they can be easily converted to a larger system of first-order differential equations.
(2) The organization of the library has been changed; all numerical methods, together with their driver programs, are now included in a single directory named LuaNumAn.
(3) The documentation has been reorganized and slightly improved.

If you are interested, here it is.

#10 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 05:43 PM

Version 1.20 is out.

This version has no new numerical methods implemented, but there are many improvements. The most important of them are:
(1) LuaNumAn now includes a plotting library, called LuaPlot. By making use of this library, graphical representation of the results has been added to most driver programs.
(2) Minor modifications in almost all numerical methods have been made, in accordance with the new features of CPLua version 0.71.
(3) The function Romberg has been modified. It is now slightly faster and more accurate.
(4) The function KroneRoots has been modified. It is now remarkably faster then the previous version (about 50% faster than in LuaNumAn version 1.00).

Here are some screenshots:

Posted ImagePosted ImagePosted ImagePosted Image

From left to right:
(1) A complex graph, demonstrating the current capabilities of the LuaPlot library (line and scatter plots, line thicknesses, point types, and grid lines). Note that this graph is created in about one second, much less than the corresponding time using the built-in "Graph" application :plol:.
(2) Graphical representation of the output of the Bisect function: The function x+e^x, and its computed root.
(3) Graphical representation of the output of the KroneRoots function: The function sin(x)-sin(5x/4), and its nine roots within the interval [-12,12], as computed by KroneRoots.
(4) Graphical representation of the output of the RK4Rich function: The solution of a system of two differential equations; thick line represents the solution for the first function, thin line the corresponding solution for the second function.

LuaNumAn can be downloaded here. The documentation has been updated, and explains all new capabilities.

#11 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 06:06 PM

A note concerning File Sharing: I was not able to upload version 1.20 in the "File Sharing" section. I tried several times without success (I always get an "Internal Server Error"). Maybe the problem is that I'm not using IE nor Windows, I don't really know, and I don't want to be bothered again by such a poor functionality (problems when uploading, plus they want only *.rar files, plus you cannot post even a single screenshot). The "File Sharing" contains the previous version of LuaNumAn, and I will not try to upload any new version there, unless the "File Sharing" section will be greatly upgraded. Until then, I don't want my programs to be available there. I tried to delete the previous version from the "File Sharing", but there is no such an option, although it was me that has uploaded the file. Therefore, I ask from a moderator to delete LuaNumAn from the "File Sharing" section. Please don't upload the new version there, I don't wish such a thing.

#12 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 28 September 2005 - 06:37 PM

(1) LuaNumAn now includes a plotting library, called LuaPlot. By making use of this library, graphical representation of the results has been added to most driver programs.

Wow! As you said, this is rather nice :nod:
It only takes one second for the first graph even with the grid, the axes etc? (I didn't take the time to try it yet) :)
I suppose you draw the grid with individual points, perhaps I should provide a faster function for that...
But If you agree I would like to convert your function into C so make it even faster ;)

#13 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 07:00 PM

Wow! As you said, this is rather nice :nod:

Be honest, it's not just nice, I think that it's very nice ;). Adding tic labels will make it almost perfect :greengrin: (please add the graph font you said in CPLua!)

It only takes one second for the first graph even with the grid, the axes etc? (I didn't take the time to try it yet) :)

Try it, to see for yourself, if you are suspicious ;). It's surprisingly fast!

I suppose you draw the grid with individual points, perhaps I should provide a faster function for that...

Indeed, the grid is drawed by a loop, which uses the draw.point function iteratively. Currently, there is no other way to proceed in CPLua :(. Adding the ability to draw dotted lines will improve speed even further. Can you add such an option in the draw.line function?

But If you agree I would like to convert your function into C so make it even faster ;)

Of course I agree. But first, read the documentation to see how the plotting functions can be used in a Lua program (they have plenty of optional arguments). Maybe you will have some useful suggestions for me.
Don't convert anything yet, because this library is changing almost everyday (in fact, whenever I have the time needed). I'm planning to add more functionality this weekend, such as logarithmic plots, minor tics and minor grid lines, maybe tic labels (but I need a function for writing text in the graph window for this ;)); I'm also planning to reorganize the library in a more convenient way. After all these improvements, I think I will stop developing the LuaPlot library, since it will be more than enough for my Numerical Analysis programs. For example, I will never try to add histograms, or pie plots, since I don't need them.

#14 2072

2072

    Casio over god

  • Admin
  • PipPipPipPipPipPipPipPip
  • 1565 posts
  • Gender:Male
  • Location:Somewherebourg
  • Interests:Alternative states of consciousness, programming, making things work the best they possibly can.

  • Calculators:
    AFX2 ROM 1.02, CFX-9940GT+, FX-180P-Plus

Posted 28 September 2005 - 07:31 PM

A note concerning File Sharing: I was not able to upload version 1.20 in the "File Sharing" section. I tried several times without success (I always get an "Internal Server Error"). Maybe the problem is that I'm not using IE nor Windows, I don't really know, and I don't want to be bothered again by such a poor functionality (problems when uploading, plus they want only *.rar files, plus you cannot post even a single screenshot). The "File Sharing" contains the previous version of LuaNumAn, and I will not try to upload any new version there, unless the "File Sharing" section will be greatly upgraded. Until then, I don't want my programs to be available there. I tried to delete the previous version from the "File Sharing", but there is no such an option, although it was me that has uploaded the file. Therefore, I ask from a moderator to delete LuaNumAn from the "File Sharing" section. Please don't upload the new version there, I don't wish such a thing.



You just piss me off saying that!!!! You should be carful I designed the Filesharing, I host casiocalc.org, I pay for the server.... And that's how you thank me! DAMN IT!!


EDIT:

I'm sorry for that crude language but at that time I was really :rant:


#15 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 07:41 PM

I designed the Filesharing, I host casiocalc.org, I pay for the server.... And that's how you thank me! DAMN IT!!

I didn't knew that an individual is paying and maintaining this server. Since this is true, it's obvious that I didn't wanted to blame an individual that does his best. But I still have problems with uploading files in the "File Sharing". I know how to do it, it's not that difficult, but it doesn't work as expected.

#16 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 28 September 2005 - 07:46 PM

(please add the graph font you said in CPLua!)

The draw.text() function is available since CPLua 0.7 ^_^
I know that I don't provide any decent documentation, but well the function's list is allways updated ;)

#17 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 07:52 PM

The draw.text() function is available since CPLua 0.7 ^_^
I know that I don't provide any decent documentation, but well the function's list is allways updated ;)

You probably added documentation for this in the function list fot version 0.71. I downloaded the file "function.txt" two days ago (during the heavy debugging night), and there is nothing about draw.text. But, since it's now available, I will try to add tic labels right now!

#18 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 28 September 2005 - 07:54 PM

You probably added documentation for this in the function list fot version 0.71. I downloaded the file "function.txt" two days ago (during the heavy debugging night), and there is nothing about draw.text. But, since it's now available, I will try to add tic labels right now!


:nonono:

UPDATES HISTORY

--- 25 sept 2005 ---

Basis:
waitkey, waitpen, input

Drawing:
draw.text

Cas:
cas.expr, cas.tostring, cas.integ

Table:
table.copy

Math:
math.sign

:greengrin:

Good work ;)

#19 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 07:58 PM

Good work ;)

Mea culpa :greengrin: :greengrin: :greengrin: .

#20 2072

2072

    Casio over god

  • Admin
  • PipPipPipPipPipPipPipPip
  • 1565 posts
  • Gender:Male
  • Location:Somewherebourg
  • Interests:Alternative states of consciousness, programming, making things work the best they possibly can.

  • Calculators:
    AFX2 ROM 1.02, CFX-9940GT+, FX-180P-Plus

Posted 28 September 2005 - 08:00 PM

I didn't knew that an individual is paying and maintaining this server. Since this is true, it's obvious that I didn't wanted to blame an individual that does his best. But I still have problems with uploading files in the "File Sharing". I know how to do it, it's not that difficult, but it doesn't work as expected.



There is a server limitation for the php scripts running on the server: they cannot run longer than 30 seconds so if your connection isn't fast enough the script get killed by the server.

I forgive you for this time. But this is not the first time you " insult" (it's not the more apropriate word but it's most close) without knowing. Just think how you would react to your own statments if they were addressed to you...

For the RAR limitation, it's because the space is not infinite on the server and RAR seems to be the best archiver available. (RAR is so much better than ZIP...)
If you don't want to use RAR you still can link your file in the FS instead of uploading it.

I know the FS needs improvements, it will be done when I'll have enough time.

#21 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 08:08 PM

There is a server limitation for the php scripts running on the server: they cannot run longer than 30 seconds so if your connection isn't fast enough the script get killed by the server.

This is probably the reason for the problems I have.

But this is not the first time you " insult" (it's not the more apropriate word but it's most close) without knowing. Just think how you would react to your own statments if they were addressed to you...

Again, I didn't wanted to blame any individual trying to maintain this forum. How should I know such a thing? But if I'm bothering you, I can stop posting anything here right now.

#22 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 28 September 2005 - 08:14 PM

Keep cool :D

I tried LuaNumAn 1.20 and this is indeed really surprising :bow:
Btw PAP, is there a real reason for you to write all your f(x) functions with this model?
function f(x)
local f
f = 1/x
return f
end
It would be quite faster to write
function f(x)
return 1/x
end
Since this is a simple tail-call :)

#23 2072

2072

    Casio over god

  • Admin
  • PipPipPipPipPipPipPipPip
  • 1565 posts
  • Gender:Male
  • Location:Somewherebourg
  • Interests:Alternative states of consciousness, programming, making things work the best they possibly can.

  • Calculators:
    AFX2 ROM 1.02, CFX-9940GT+, FX-180P-Plus

Posted 28 September 2005 - 08:15 PM

But if I'm bothering you, I can stop posting anything here right now.


You did bothered me today but it already belongs to past.

Just have a little more empathy when you criticise something and everything will be OK :)

#24 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 08:33 PM

I tried LuaNumAn 1.20 and this is indeed really surprising :bow:

Isn't it? CPLua permits to write such a neat code, that respects the data hiding, and runs remarkably fast. Amazing, for a calculator :). CPLua really rocks!

Btw PAP, is there a real reason for you to write all your f(x) functions with this model?

function f(x)
local f
f = 1/x
return f
end
It would be quite faster to write
function f(x)
return 1/x
end
Since this is a simple tail-call :)

I saw plenty of examples that use this model, so I thought that it is imperative to define every returning variable as local. What if you don't? in the example above, f will be a global (to the chunk) variable then? What if the function is more complex than a single statement? I didn't printed the manual yet, my printer is out of ink.

#25 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 28 September 2005 - 08:52 PM

I didn't rewrite the local keyword for the function declaration, but naturally you may write it. Note that this keyword is useless in that case, since this function will be local to the current chunk, and this is the actual behaviour of any variable inside the chunk if you don't export it explicitely (you were the one asking for that ;))
But there is an advantage to not create a local variable inside the function, because the lua interpreter won't have to allocate a new block of memory to create this temporary variable. You can return the correct value without using a variable for that. You can use a local variable naturally, but here the only consequences are
- time consuming
- memory consuming
- bigger code ;)

(Note that the temporary variables are not deleted immediately after they become unavailable. The garbage collector need to be called explicitely if the total used memory didn't reach the threshold <_< This is a really annoying behaviour, and I will set a workaround to call the GC periodically.)

Finally, a function's returned value does not have to be local. But if it is not, it will be considered as local to the chunk, and thus it will be accessible outside the function... This could be confusing and thus the local keyword is generaly justified for variable inside functions.

I hope that it was clear enough :unsure:

Edit: Btw, your Plot library takes more than 30 Kbs of memory just by loading it 8O This is a long code but still, this is annoying. I definitely need to pay more attention on the memory management for the next releases :nonono:

#26 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 28 September 2005 - 10:24 PM

I didn't rewrite the local keyword for the function declaration, but naturally you may write it. Note that this keyword is useless in that case, since this function will be local to the current chunk, and this is the actual behaviour of any variable inside the chunk if you don't export it explicitely (you were the one asking for that ;))

Unfortunately, nobody asked for this, except me. Don't tell me that the code isn't clearer if you respect the data hiding?

But there is an advantage to not create a local variable inside the function, because the lua interpreter won't have to allocate a new block of memory to create this temporary variable. You can return the correct value without using a variable for that. You can use a local variable naturally, but here the only consequences are
- time consuming
- memory consuming
- bigger code ;)

The local declaration is generally not useless: it isolates the variable f from the rest of the chunk. But you are right, in this case, I can simply write return -1/x. I wrote it this way because I wanted to modify the functions to something more complex, e.g., the Bessel functions; in this case, the function will need many temporary variables, and it is dangerous to declare such variables as global, even if they are global in the chunk only. Naturally, variable allocation/deallocation is time and memory consuming, but I prefer this instead of "chunk-global" variables. In these simple functions, however, a returning variable is not needed at all. I'll remove useless variables in all the examples that use simple functions.

(Note that the temporary variables are not deleted immediately after they become unavailable. The garbage collector need to be called explicitely if the total used memory didn't reach the threshold <_< This is a really annoying behaviour, and I will set a workaround to call the GC periodically.)

I have read about the garbadge collector in the manual. Its behavior is strange indeed. See what you can do for this, and let us know. Maybe there is a way to shorten the threshold?

the local keyword is generaly justified for variable inside functions.

Indeed. I totally agree, except for the word "generally"; I would say "always" instead ;).

I hope that it was clear enough :unsure:

More than clear, thanks!

Edit: Btw, your Plot library takes more than 30 Kbs of memory just by loading it 8O This is a long code but still, this is annoying. I definitely need to pay more attention on the memory management for the next releases :nonono:

Hmm, the CPLua source code is probably long enough now, maybe it needs some kind of clean-up? I have made many (small) improvements in my programs, just by deciding to "clean them up".

#27 Kilburn

Kilburn

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 491 posts
  • Gender:Male
  • Location:France
  • Interests:Blah

  • Calculators:
    FX-7500 G
    ClassPad 300

Posted 05 October 2005 - 04:07 PM

I have made a Graph library similar than yours: Lua Graph Library 1.00

Here are the functions:

* graph.show()  Shows the graph window
* graph.refresh()  Clears the graph window and retraces the axes and the grid
* graph.setaxes(set)  set=1:Enable axes  set=0:Disable axes
* graph.setgrid(set) set=0:Disable grid  set=1:Enable grid (points)  set=2:Enable grid (dotted lines)
* graph.viewwindow(xmin,xmax,xscl,ymin,ymax,yscl) Sets the window settings
* graph.plot(x,y) Draws a point in graphical coordinates
* graph.line(x1,y1,x2,y2) Draws a line in graphical coordinates
* graph.splot(x,y,type) Draws a special point. Reserved for stat graphs
* graph.vmesh(x) Draws a vertical dotted line
* graph.hmesh(y) Draws an horizontal dotted line
* graph.update() = draw.update()

* graph.drawfunc(func[,line=THIN,min=xmin,max=xmax,show=NORMAL]) Draws a "y(x)=" function
 line=PLOT, THIN or THICK : Type of line
 min,max : Lower and upper X boundaries.
 show=NORMAL : Trace a graph as on the Graph application
 show=SHOWBUFFER : Shows the graph only when the tracing finishes
 show=KEEPBUFFER   : Don't show the graph. You must call graph.update() to show it.
* graph.drawinv(func[,line=THIN,min=ymin,max=ymax,show=NORMAL]) Draws a "x(y)=" function
 min,max : Lower and upper Y boundaries.
* graph.drawpol(func,min,max,step[,line=THIN,show=NORMAL]) Draw a "r(th?ta)=" function
 min,max,step : Start angle, end angle, angle pitch.
 line,show : Explained in the previous functions.
* graph.drawparam(func,min,max,step[,line=THIN,show=NORMAL]) Draw a "x(t),y(t)=" function
 min,max,step : Start T, end T, T pitch.
  Important:Declare the function as below:
   function f1(t)
	(what you want...)
	return x,y
   end
* graph.drawstat2v(xdata,ydata[,point=DOT,line=THIN,show=NORMAL]) Draws a simple two-variable graph
 xdata,ydata : Lists used to trace the graph
 point : Point type : DOT : dot
							 LDOT : large dot
							 SQUARE : square
							 CROSS : cross
							 CIRCLE : circle
							 CIRCLE2 : circle with a cross inside of it
 line : Line type : PLOT,THIN,THICK
* graph.plotfunc(f,table[,point,line,show]) Draws a function using a table.
 table : X Values

* gTbl(start,end,step) Generates a table to use in graph.plotfunc()

I hope that PAP won't be angry with me : I didn't need to read his source code to write this! ;)

#28 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 05 October 2005 - 04:16 PM

Version 1.30 is out.

This version has two new numerical methods, and several minor improvements:
(1) Minor modifications have been made, in accordance with the new features of CPLua version 0.72.
(2) Four utility functions, concerning matrix operations, have been added.
(3) A method for computing the LU decomposition of a matrix has been added. This permits to solve a system of linear equations, and compute the determinant or the inverse of a matrix.
(4) A method for non-linear fitting of data using the Levenberg-Marquardt algorithm has been added. The corresponding LuaNumAn function is called LMfit.
(5) Plotting functions now can use labels.

Here are some screenshots:

Posted ImagePosted ImagePosted ImagePosted Image

From left to right:
(1) A set of data measures with a randomly selected error, and the curve that models the data. The fitting function has 3 parameters, computed by LMfit.
(2) The error of the fitting function (1), compared to the true function that describes the data.
(3) As in (1), but with a larger set of data, corresponding to a more complex function (two Gaussian curves). The fitting function has 6 parameters, also computed by LMfit.
(4) The error of the fitting function (3).

The new version can be downloaded here. As usual, the documentation has also been updated, explaining all new features.

Note: For some reason, the results taken with the executable are slightly different with those taken by ClassPad, although the same "random seed" is used to generate the data points. The difference is notable only as far as the error is concerned (it remains small, but has a different curve). I suspect that the PC executable uses a different version of math.random, hence the difference. Orwell can you say something about this?

#29 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 05 October 2005 - 04:38 PM

I hope that PAP won't be angry with me

No reason to be angry. The source code is open, you can read it or not.
To be honest, I don't like the fact that your implementation requires to call several functions to make a plot; but this is only a matter of personal taste. However, your implementation, supports polar and parametric plots, which is a nice feature. Btw, I don't think that I will replace my plotting functions with this, but where is the source code? You are just announcing that you wrote a plotting library or you are planning to release it? If so, making a new topic for this will be preferable.

I didn't need to read his source code to write this! ;)

Of course you didn't. I was never planning to write a complete graphics library, I only wanted to visualize the results obtained by the numerical methods I implemented in LuaNumAn. Btw, for these numerical methods, you do need to read the source code, or several Numerical Analysis books. It's not that easy :P.

#30 Kilburn

Kilburn

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 491 posts
  • Gender:Male
  • Location:France
  • Interests:Blah

  • Calculators:
    FX-7500 G
    ClassPad 300

Posted 05 October 2005 - 04:55 PM

I have made a Graph library similar than yours: Lua Graph Library 1.00


"Lua Graph Library" is a link to the file!!!

I will try to combine your source code with mine to make a more powerful library.

#31 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 05 October 2005 - 05:16 PM

"Lua Graph Library" is a link to the file!!!

Sorry, I didn't noticed it. My bad.

I will try to combine your source code with mine to make a more powerful library.

Good luck. Again, creating a new topic for this will be a preferable.

#32 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 05 October 2005 - 08:19 PM

Actually I was also thinking about a "graph" library written in C... But it's an excellent thing that you both already decided to create it in Lua. :)
Maybe we should try to define a "standard" interface, by thinking about the more convenient way to present the different functions of the library etc, thus later someone will just have to respect this standard and rewrite those functions in C :D

I suspect that the PC executable uses a different version of math.random, hence the difference. Orwell can you say something about this?

I really don't know. :blink: The code of both versions is exactly the same, but maybe the C random function differs from the platform...

#33 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 05 October 2005 - 08:43 PM

Actually I was also thinking about a "graph" library written in C...

That's the ultimate goal, but it can wait. In my opinion, plenty of things have higher priority.

Maybe we should try to define a "standard" interface, by thinking about the more convenient way to present the different functions of the library etc, thus later someone will just have to respect this standard and rewrite those functions in C :D

Totally agree (provided that the "standard" interface will be influenced by my implementation, of course :plol:).
Now, speaking more seriously, I think that using "general purpose" plotting functions is much better than using several auxiliary functions that should be combined to produce a graph. There is, of course, a disadvantage: each general purpose plotting function will have plenty of optional arguments (see my PlotFunc and PlotData functions, they have 7 and 9 optional arguments, respectively). Nevertheless, I still think that making a complex graph by using just one (or two) functions is preferable than calling plenty of auxiliary functions for setting the scaling, the axes, etc. Afterall, you don't need to use all these optional arguments often.

I really don't know. :blink: The code of both versions is exactly the same, but maybe the C random function differs from the platform...

That's what I wanted to say. Random (to be more precise, "pseudo-random") generators are usually proccessor-dependent. That's probably the reason for the differences between ClassPad and the executable. It's not important, anyway.

#34 BiTwhise

BiTwhise

    Casio Overgod

  • [Legends]
  • PipPipPipPipPipPipPip
  • 627 posts
  • Gender:Male
  • Location:Guildford, Surry, UK
  • Interests:Programming, games, consoles, martial arts

  • Calculators:
    FX9700WE, CFX9970G, AFX2.0, Classpad 300

Posted 05 October 2005 - 09:29 PM

That's what I wanted to say. Random (to be more precise, "pseudo-random") generators are usually proccessor-dependent. That's probably the reason for the differences between ClassPad and the executable. It's not important, anyway.


The standard C rand() function is dependant on the compiler vendors library implementation. Although some newer CPUs have random number generators for more secure cryptation etc, I don't think most C/C++ implementation make use of these (correct me if I'm wrong). I assume the lua source uses this standard C function for it's random number generation.

If you want something that's consistent across platforms, write it yourself, or use some open source pseudo random number generator like the Mersenne Twister (generates reasonably fast, and probably gives you better distribution than the standard C one).


Now, speaking more seriously, I think that using "general purpose" plotting functions is much better than using several auxiliary functions that should be combined to produce a graph. There is, of course, a disadvantage: each general purpose plotting function will have plenty of optional arguments (see my PlotFunc and PlotData functions, they have 7 and 9 optional arguments, respectively). Nevertheless, I still think that making a complex graph by using just one (or two) functions is preferable than calling plenty of auxiliary functions for setting the scaling, the axes, etc. Afterall, you don't need to use all these optional arguments often.

I'd prefer having more granular functions, and exposing some of these internal mechanism of things to the coder.
You can still provide functions that take in arguments for everything necessary to plot the graph. These would naturally route on the auxiliary functions, which in turn should lead to cleaner code internally in the graph library as well.

#35 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 06 October 2005 - 07:26 AM

If you want something that's consistent across platforms, write it yourself, or use some open source pseudo random number generator like the Mersenne Twister (generates reasonably fast, and probably gives you better distribution than the standard C one).

Actually, I was thinking to implement a random number generator based on the Park-Miller-Marsaglia scheme. It's simple enough, but I'm not sure about the speed. I don't know anything about the Mersenne-Twister generator, maybe it will be faster. But one thing is sure, C's "rand" is rather primitive, compared to the random number generator provided by Fortran 95.
Anyway, I haven't included in LuaNumAn something that realy needs a powerful random generator, and I'm not planning to do so, at least in the near future (this is mostly needed in Monte-Carlo methods, which are out of my domain of interest). I noticed the difference between the executable and the ClassPad in an example, where random numbers are simply used to generate a set of data to be fitted by a theoretical curve.

I'd prefer having more granular functions, and exposing some of these internal mechanism of things to the coder.
You can still provide functions that take in arguments for everything necessary to plot the graph. These would naturally route on the auxiliary functions, which in turn should lead to cleaner code internally in the graph library as well.

Maybe the best way to proceed is making available to the user both "granular" and "high-level" plotting functions, both written in C. The reason is simple: A "high-level" function written in Lua will be definitely slower. In fact, granular functions are called by my plotting functions, but I decided to make them hidden from the user, since, in a Numerical Analysis program, you usually want to visualize what your numerical method gives as a result, and for that, the user don't need to be bothered with the internal mechanism of plotting. In most cases, plotting functions are needed for something similar. It is annoying to use plenty of granular functions in order to make a plot (it reminds me the way you plot something in CPBasic). Of course, internally, granular functions are used, maybe we can make them available to the user too, but the user should have the option to use granular functions or not. So, we have to provide a simple way to generate powerful graphs: issue a plotting function that can be used with a limited set of mandatory arguments, and if (and only if) you want to set auxiliary settings, use the optional arguments for this.

Pa gjensyn (I think that in norwegian it means something like "see you")

#36 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 06 October 2005 - 11:30 AM

I already used (for another project) a good C++ library to generate random numbers (with uniform or normal distribution), I can reuse it in CPLua if you want ;) I needed it to implement a Monte-Carlo method btw.

Feel free to talk about the way the future C++ 'graph' package for CPLua should be organized :nod:

#37 BiTwhise

BiTwhise

    Casio Overgod

  • [Legends]
  • PipPipPipPipPipPipPip
  • 627 posts
  • Gender:Male
  • Location:Guildford, Surry, UK
  • Interests:Programming, games, consoles, martial arts

  • Calculators:
    FX9700WE, CFX9970G, AFX2.0, Classpad 300

Posted 06 October 2005 - 07:53 PM

Maybe the best way to proceed is making available to the user both "granular" and "high-level" plotting functions, both written in C. The reason is simple: A "high-level" function written in Lua will be definitely slower. In fact, granular functions are called by my plotting functions, but I decided to make them hidden from the user, since, in a Numerical Analysis program, you usually want to visualize what your numerical method gives as a result, and for that, the user don't need to be bothered with the internal mechanism of plotting. In most cases, plotting functions are needed for something similar. It is annoying to use plenty of granular functions in order to make a plot (it reminds me the way you plot something in CPBasic). Of course, internally, granular functions are used, maybe we can make them available to the user too, but the user should have the option to use granular functions or not. So, we have to provide a simple way to generate powerful graphs: issue a plotting function that can be used with a limited set of mandatory arguments, and if (and only if) you want to set auxiliary settings, use the optional arguments for this.

Sounds like we're pretty much saying the same thing ^_^

Pa gjensyn (I think that in norwegian it means something like "see you")

Nesten, samme som fransk Au Revoir :)

Av nysgjerrighet, hvor (og hvorfor) har du l?rt ? snakke norsk?

#38 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 06 October 2005 - 09:20 PM

Av nysgjerrighet, hvor (og hvorfor) har du l?rt ? snakke norsk?

Jeg liker Knut Hamsuns boker, spesielt de utmerkete bokene "Sult" og "Barn av tiden", som er virkelige mesterstykker. Imidlertid er det meget vanskelig til meg: de skrevet i Bokm?l. :(
(Jeg er sikker p? at jeg har lager mange feil i denne teksten. Jeg er virkelig trist, det er det beste som jeg gjor...)

#39 Guest_Guest_*

Guest_Guest_*
  • Guests

Posted 07 October 2005 - 03:00 PM

I must tell You Lua is cure for all my problems with classpad. Since I bought CP I had always problems with speed. Now they finished. Thank You very much! and I hope that Your project will be wider and wider

#40 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 07 October 2005 - 03:46 PM

I must tell You Lua is cure for all my problems with classpad. Since I bought CP I had always problems with speed. Now they finished. Thank You very much!

We all had problems with the ClassPad, and, indeed, now they have finished. :)
Thank you for your compliments, it really makes me happy. But most thanks should go to Orwell, who develops CPLua. Afterall, without CPLua this project will not be possible.

and I hope that Your project will be wider and wider

It will, be sure for that: I'm planning to add more numerical methods so that LuaNumAn will eventually become a complete Numerical Analysis package. I started this project because I needed such a package in a portable calculator, and because programming in CPLua is a real pleasure. The fact that CPLua permits to develop even complex mathematical programs in a calculator is really impressive. :)


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users