# Cplua Funcs Library

14 replies to this topic

### #1 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 09 January 2007 - 03:25 PM

This topic is opened for user-created function in Lua.

Let's start!

Function 1: mod( a , n , b)

::Return: mod(a^n,

::Example:

::-print(mod(2007,321123,332211)) -------> 285663

or 2007^321123285663 (mod 332211)

::-print(mod(2,123456789,2007)) --------> 1673

or 2^1234567891673 (mod 2007)

::-print(mod(5,200,"10^10")) --------> 2900390625

or 10 last digits of 5^200 is 2900390625 /* You must input "10^10", not 10^10! Unless it will cause an error, I dont know why (try print(10^10) in CPLua)/*

::Known bugs:none

::Author: vanhoa

::Ver: 1.0

::Code:
`require("cas") function mod(a,n,c) c=cas(tostring(c)) if c==cas("0") then return "How to devide by zero?!" end if cas.abs(c)==cas.abs("1") then return cas(0) end local dec2bin=function(x) local re={} repeat if cas.mod(x,2)==cas("1") then x=cas("(x-1)/2|x="..x) re[#re+1]=1 else x=cas("x/2|x="..x) re[#re+1]=0 end until x==cas("0") return re end local pow2,bin,re={cas("mod("..a..","..c)},dec2bin(cas(tostring(n))),1 for i=2,#bin do pow2[i]=cas("mod("..pow2[i-1].."^2,"..c) end for i=1,#bin do if bin[i]==1 then re=cas("mod("..re.."*"..pow2[i]..","..c) end end return cas(re) end export{mod=mod}`

### #2 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 09 January 2007 - 04:49 PM

Function 2 and 3: pi(n) , divide(a,b,n)

::Example:

> print(pi(22))
3.1415926535897932384626
> print(divide(123456789,999999999,100))
0.123456789123456789123456789123456789123456789123456789123456789123456789123456
7891234567891234567891
> print(pi(100))
3.141592653589793238462643383279502884197169399375105820974944592307816406286208
9986280348253421170656
>

::Code:
`require "cas" function divide(a,b,digit) a=cas(tostring(a)) b=cas(tostring(b)) si=cas.signum(a.."*"..b) if a==cas("0") then return cas(0) end if b==cas(0) then return "Division by zero" end a=cas.abs(a) b=cas.abs(b) local kq=cas.int(a.."/"..b) if cas(a)==cas(kq.."*"..b) then return kq end kq=tostring(kq).."." local pl=kq a=cas("10("..a.."-"..pl.."*"..b) for i=1,digit do pl=cas.int(a.."/"..b) kq=kq..pl a=cas("10("..a.."-"..pl.."*"..b) end if si==cas("-1") then kq="-"..kq end return kq end function pi(n) local temp=cas("1/64*\238\81((-32/(4x+1)-1/(4x+3)+256/(10x+1)-64/(10x+3)-4/(10x+5)-4/(10*x+7)+1/(10*x+9))*(-1)^x/1024^x,x,0,int("..n.."/\238\4)") return divide(cas.numerator(temp),cas.denominator(temp),n) end export{pi=pi,divide=divide}`

### #3 Casio Calculator Factory

Casio Calculator Factory

Newbie

• Members
• 29 posts
• Location:Bolivia
• Interests:La Programaci?n es mi hobby preferido desde las aparatos alectricos peque?os hasta Programas para PC.

• Calculators:
<<<<Algebra FX>>>>
<<CFX-9850G series>>
<<<<hp-48 hp-49>>>>

Posted 10 January 2007 - 12:29 AM

Good Job!!!

### #4 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 12 July 2007 - 05:30 AM

I've made a functon to give the critical point of any inputted function.

Definition: Let f be a function of n variables x1, x2,... xn defined on an open domain in the n-dim plane. A point P(x00,x01,...,x0n) is a critical point for f if f is differentiable at P and if fx0(x00,x01,...,x0n) = fx1(x00,x01,...,x0n) = ... = fxn(x00,x01,...,x0n) = 0 where fxi is diff(f,xi).

Examples: Let f(x,y)=x^2+y^2, then fx=2*x and fy=2*y, so the function has a single critical point (0,0)...

What happens if f(x,y)=9-x^2-y^2 and a condition, -3<=x<=3 and -3<=y<=3 for examle, it will return (0,0) as a critical point (and then f_max=9, so my program now is quite limitted), but I want it return x=+-3, y=+-3 (and then the local minimum of f in [[-3;-3];[3;3]] is 0), how can I know this?

### #5 Orwell

Orwell

Casio Overlord

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

• Calculators:
Casio AFX 1.02 / Casio ClassPad 300

Posted 12 July 2007 - 09:52 PM

What happens if f(x,y)=9-x^2-y^2 and a condition, -3<=x<=3 and -3<=y<=3 for examle, it will return (0,0) as a critical point (and then f_max=9, so my program now is quite limitted), but I want it return x=+-3, y=+-3 (and then the local minimum of f in [[-3;-3];[3;3]] is 0), how can I know this?

You may not do that, because the function is not differentiable on the border of the defined domain; thus a point on the border will never be a solution

### #6 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 13 July 2007 - 01:26 AM

Ya we dont need to calc the diff of that func on he border, we just need to find the part diff of that func along the border and then com pare them all: the value of 4 corners, the local critical point on the border and the local critical point of the function...

I've got an other proble... The built-in solve() function is quite weak, but I dont want to solve this numerically...

### #7 Orwell

Orwell

Casio Overlord

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

• Calculators:
Casio AFX 1.02 / Casio ClassPad 300

Posted 13 July 2007 - 11:17 AM

I didn't say we needed to calculate the diff on the border, I said the function was not differentiable there, by definition.

You said yourself in the definition that one of the conditions for P to be a critical point for a function F if that F must be differentiable at P, and mathematically a function is never differentiable on its border, that's all

### #8 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 13 July 2007 - 12:25 PM

Opps, You didnt understand me...

I will give an example:

Let f(x,y) is x^2+y^2, where -2<=x,y<=3

* We have fdx=2x and fdy=2y, they are all zero when x=y=0.

* On the border:
+/ let g(x) = f(x,-2) then f(x)=x^2+4, its diff is 2x... g's minimun value is 4 and maximun is Max(g(-2),g(3))=9
...

### #9 Orwell

Orwell

Casio Overlord

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

• Calculators:
Casio AFX 1.02 / Casio ClassPad 300

Posted 13 July 2007 - 01:51 PM

I suggest you to look for a definition of differentiable, as well.

As long as there is a direction where you cannot calculate the diff at a point, your function is not differentiable at that point.

Be careful, "differentiable" is a very strong constraint. It does not simply mean "it's possible to calculate a diff there".

### #10 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 14 July 2007 - 03:54 AM

I know that we can diff a func at its border... But I'm not talking about it :/

### #11 Hobart

Hobart

Casio Fan

• Members
• 34 posts
• Gender:Male
• Location:Germany (Saxony/Ore Mountains)

• Calculators:

Posted 16 July 2007 - 11:08 AM

Well, I've made some functions, that uses complex numbers, because CPLua doesn't support it (yet).

[codebox]
function incC(c1,c2)
return {c1+c2,c1+c2}
end

function decC(c1,c2)
return {c1-c2,c1-c2}
end

function mulC(c1,c2)
return {c1*c2-c1*c2,c1*c2+c1*c2}
end

function divC(c1,c2)
return {(c1*c2+c1*c2)/(c2^2+c2^2),(c1*c2-c1*c2)/(c2^2+c2^2)}
end

function powC(c1,n)
local ctmp = {1,0}
for i = 1,n do
ctmp = mulC(ctmp,c1)
end
return ctmp
end

function sqrtC(c1)
return {.5*(2*(c1+(c1^2+c1^2)^.5))^.5,.5*(-2*(c1-(c1^2+c1^2)^.5))^.5}
end

function absC(c1)
return {(c1^2+c1^2)^.5,0}
end
[/codebox]

The complex numbers are only tables with two fields: the real and the complex part.
A function that returns a string containing the correct form I made but I'll improve it first. I will post it soon.
I hope there are no bugs...

### #12 Hobart

Hobart

Casio Fan

• Members
• 34 posts
• Gender:Male
• Location:Germany (Saxony/Ore Mountains)

• Calculators:

Posted 16 July 2007 - 12:33 PM

Here is the described function:

`function tocompstr(c1) local s1,s2 if c1[2]==0 then  s2 = "" elseif c1[2]==1 then  s2 = "\238\1" elseif c1[2]==-1 then  s2 = "-\238\1" else  s2 = tostring(c1[2]).."\238\1" end if c1[1]==0 then  s1 = ""  if c1[2]==0 then   return "0"  end else  s1 = tostring(c1[1])  if c1[2]>0 then   s2 = "+"..s2  end end return s1..s2end`
And of course the export of all the functions (I forgot it the time before...) :
```export{incC=incC,decC=decC,mulC=mulC,divC=divC,powC=powC,sqrtC=sqrtC,absC=absC,t
ocompstr=tocompstr}```
I think it works!

### #13 girdeux

girdeux

• Members
• 88 posts
• Location:Spain / Castell?n

• Calculators:
casio fx-115ms;

Posted 22 July 2007 - 01:19 PM

yeah this is some useful, only 2 things:

- I think that you have to mutiplicate the imaginary part by (-1) in the division complex function.
- Why pwrC(c1,0.5) =~ sqrtC(c1)??

Ok if the autor of the program (Hobart) lets me I have modified the program:

I have corrected the first bug (*-1), I have added the arg, conj and comptoexp functions, I have joined all the functions in 2 functions:

Comp2(c1,'+',c2)
Comp2(c1,'-',c2)
Comp2(c1,'*',c2)
Comp2(c1,'/',c2)
Comp2(c1,'^',n)
Comp1(c1,'sqrt')
Comp1(c1,'abs')
Comp1(c1,'arg')
Comp1(c1,'conj')
Comp1(c1,'comptotri') {a,b} -> a+bi
Comp1(c1,'comptoexp') {a,b} -> abs({a,b})*e^(arg({a,b})*i)

this is the code

[codebox]
function Comp2(c1,op,c2)

if #c1~=2 or #c2~=2 then
return print('Invalid dimension')
end

if op=='+' then
return {c1+c2,c1+c2}

elseif op=='-' then
return {c1-c2,c1-c2}

elseif op=='*' then
return {c1*c2-c1*c2,c1*c2+c1*c2}

elseif op=='/' then
return {(c1*c2+c1*c2)/(c2^2+c2^2),-(c1*c2-c1*c2)/(c2^2+c2^2)}

elseif op=='^' then
local ctmp = {1,0}
for i = 1,c2 do
ctmp = Comp2(ctmp,'*',c1)
end
return ctmp

else
return print('Invalid operation')
end

end

function Comp1(c1,op)

if #c1~=2 then
return print('Invalid dimension')
end

if op=='sqrt' then
return {.5*(2*(c1+(c1^2+c1^2)^.5))^.5,.5*(-2*(c1-(c1^2+c1^2)^.5))^.5}

elseif op=='abs' then
return (c1^2+c1^2)^.5

elseif op=='arg' then

if c1==0 and c1==0 then return 0 end

local a
a=c1/c1

if c1>=0 then
return
math.atan(a)
elseif c1>=0 then
return
math.atan(a)+math.pi
else return
math.atan(a)-math.pi
end

elseif op=='conj' then
return
{c1,-c1}

elseif op=='comptotri' then

if c1==0 then
return printf('%.4f \n',c1)

else

if c1<0 then
return printf('%.4f%.4f \238\1 \n',c1,c1)
else
return printf('%.4f+%.4f \238\1 \n',c1,c1)
end

end

elseif op=='comptoexp' then

if c1==0 and c1>0 then return
printf('%.4f \n',tostring(Comp1(c1,'abs')))
else
return printf('%.4f\238\2^(%.4f\238\1) \n'
,tostring(Comp1(c1,'abs'))
,tostring(Comp1(c1,'arg')))
end

else
return print('Invalid operation')
end

end

export{Comp1=Comp1,Comp2=Comp2}
[/codebox]

EDIT: I have edited a bug in my code.

### #14 Hobart

Hobart

Casio Fan

• Members
• 34 posts
• Gender:Male
• Location:Germany (Saxony/Ore Mountains)

• Calculators:

Posted 26 July 2007 - 07:09 PM

- I think that you have to mutiplicate the imaginary part by (-1) in the division complex function.

Oops! I'm sorry I didn't notice that.

- Why pwrC(c1,0.5) =~ sqrtC(c1)??

I used the 'for' loop to match the 'powC' function. So you can only use integers greater than 0.

But it's good that you improved it. This is the sense of the whole thing!

### #15 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 27 July 2007 - 04:46 AM

Function sinh(X)
return (Exp(X) - Exp(-X)) / 2
End

Function cosh(X)
return (Exp(X) + Exp(-X)) / 2
End

Function tanh(X)
return (Exp(2 * X) - 1) / (Exp(2 * X) + 1)
End

function clog(r,i)
local retvar = { ln(r^2+i^2)/2 , 0 }
If Abs® > Abs(i) Then
retvar = Sgn(i) * (Sgn® * Atn(Abs(i / r)) - (Sgn® - 1) * 2 * Atn(1))
If i == 0 And r < 0 Then retvar = 4 * Atn(1)
Else
retvar = Sgn(i) * (Sgn(-r) * Atn(Abs(r / i)) + Sgn(i) * 2 * Atn(1))
End
return retvar,retvar
end

function cexp(r,i)
return cos(i) * ( cosh®+sinh® ) , Sin(i) * ( cosh® + sinh® )
end

and then a^b = e^(b*ln(a)) (a,b are complex)

I calculate+guess the functions myself so they may be wrong

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users