Monday, July 26, 2021

#1 2009-12-10 12:54:32 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

cos, sin, tan?

Can't find any language for this.

Offline

 

#2 2009-12-10 02:17:53 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: cos, sin, tan?

Use the Mathematical Functions which is part of Satimage.osax


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#3 2009-12-10 08:32:59 pm

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: cos, sin, tan?

If you have Excel, you could use that function or you could use the series expansion to calculate it.

Applescript:

on Sin(x)
   set maxLoop to 19
   set factorial to 1
   set retVal to 0
   repeat with i from 1 to maxLoop by 2
       set term to (-1 ^ ((i mod 4 - 1) / 2)) * (x ^ i) / factorial
       set retVal to retVal + term
       set factorial to factorial * (i + 1) * (i + 2)
   end repeat
   return retVal
end Sin

on xlSin(x)
   tell application "Microsoft Excel"
       return (execute excel 4 macro VB macro name "SIN(" & x & ")")
   end tell
end xlSin

Cos x = 1 - x^2/2 + x^4/4! - x^6/6! + ...


Filed under: microsoft

Offline

 

#4 2009-12-11 03:46:49 am

Mark67
Member
Registered: 2005-12-22
Posts: 780

Re: cos, sin, tan?

JavaScript is supposed to be very good at this kind of Math (Im not BTW much has been forgotten since school) so you could always have Safari help you out doing the math by doing its JavaScript and passing you back the result (I only say this as you should have this app installed there are other apps that can do JavaScript too so check) This should give you some idea anyhow…

Applescript:

set JS to "function getAngle() {
// Should have right angle triangle
var adjSide = 10;
var oppSide = 16;
var myAngle = (Math.atan(oppSide / adjSide)) * (180 / Math.PI);
return myAngle;
};
getAngle();"


tell application "Safari"
   activate
   set MyMath to do JavaScript JS in front document
   display dialog the result
end tell


Filed under: safari

Offline

 

#5 2009-12-11 05:50:22 am

clemhoff
Member
From:: Paris (France)
Registered: 2006-08-08
Posts: 82

Re: cos, sin, tan?

Hi,

...or simply using awk's arithmetic built-in functions

Applescript:

-- - available functions
-- - cos(x); exp(ex); int(x); log(x) ; sin(x); sqrt(x); atan2(y,x); rand(); srand(x)

set fct to "cos"
set angle to 30 -- in degree

set x to (do shell script "awk 'BEGIN{ pi = 4.0*atan2(1.0,1.0); degree = pi/180.0; print " & fct & "(" & angle & "*degree)}'") as real

if you need precision use the printf  statement:

Applescript:

set fct to "cos"
set angle to 30 -- in degree
set precision to 12 -- interger

set x to (do shell script "awk 'BEGIN{ pi = 4.0*atan2(1.0,1.0); degree = pi/180.0; calc=" & fct & "(" & angle & "*degree); printf(\"%." & precision & "f\\n\", calc)}'") as real

and to calculate the tangent:

Applescript:

set angle to 30 -- in degree
set precision to 12 -- interger

set |tan| to do shell script "awk 'BEGIN{
pi = 4.0*atan2(1.0,1.0)
degree = pi/180.0
sinus = sin("
& angle & " * degree)
cosinus = cos("
& angle & " * degree)
print sinus/cosinus
}'"

you could also use the bc's math library:

Applescript:

-- s (x) -- sine of x, x in radians.
-- c (x) -- cosine of x, x in radians.
-- a (x) -- arctangent of x, arctangent returns radians.
-- l (x) -- natural logarithm of x.
-- e (x) -- exponential function of raising e to the value x.
-- j (n,x) -- bessel function of integer order n of x.

set |angle°| to 60
set fct to "c"
set precision to 13 -- interger

set y to do shell script "echo \"scale=" & precision & "; " & fct & "(" & |angle°| & " * (4 * a(1) / 180))\" | bc -l"

hth

Last edited by clemhoff (2009-12-11 01:47:51 pm)

Offline

 

#6 2021-06-15 06:52:59 pm

Honza
Member
Registered: 2021-02-14
Posts: 1

Re: cos, sin, tan?

Or simply (a bit more concise version of Mark67's answer):

Applescript:


on math(f, x)
   return run script "Math." & f & "(" & x & ")" in "JavaScript"
end math

on sin(x)
   math("sin", x)
end sin

on cos(x)
   math("cos", x)
end cos

on random()
   math("random", "")
end random

log "sin(pi as real / 6): " & sin((pi as real) / 6) (*sin(pi as real / 6): 0.5*)
log "cos(pi as real / 6): " & cos((pi as real) / 6) (*cos(pi as real / 6): 0.866025403785*)
log "random(): " & random() (*random(): 0.687339054003*)

And similarly for abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, clz32, cosh, exp, expm1, floor, fround, log, log1p, log10, log2, round, sign, sin, sinh, sqrt, tan, tanh, and trunc.

As atan2, imul, and pow need 2 parameters, we could have some math2(f, x, y) method.

As hypot, max, and min can have many parameters in JavaScript, we need a mathN(f, list_), too, and listToString() in order to concatenate the list using ",":

Applescript:


on listToString(list_)
   set AppleScript's text item delimiters to ", "
   set string_ to list_ as text
   set AppleScript's text item delimiters to {""} --> restore delimiters to default
   string_
end listToString

on mathN(f, list_)
   run script "Math." & f & "(" & parameters & ")" in "JavaScript"
end mathN

on max(list_)
   mathN("max", list_)
end max

on min(list_)
   mathN("min", list_)
end min

log "max({5,9,3,10,4}): " & max({5, 9, 3, 10, 4}) (*max({5,9,3,10,4}): 10*)
log "min({5,9,3,10,4}): " & min({5, 9, 3, 10, 4}) (*min({5,9,3,10,4}): 3*)

(Note that mathN could be used for the methods with just one parameter and of course also for those that need two, e.g., mathN("sin", {x}) and mathN("pow", {x, y}...)

There are some other cool possibilities of integrating JavaScript within AppleScript:

1) Sorting a list is certainly quite useful and while for instance the simple_sort at https://www.macosxautomation.com/apples … rt-05.html has ~20 lines in pure AppleScript, with JavaScript it can be done on a single line in pretty straightforward way:

Applescript:


on sort(list_)
   run script "[" & listToString(list_) & "].sort((n1, n2) => n1 - n2)" in "JavaScript"
end sort

log "sort({5, 9, 3, 10, 4}): " & listToString(sort({5, 9, 3, 10, 4})) (*sort({5, 9, 3, 10, 4}): 3, 4, 5, 9, 10*)

2) Let's now be a bit more adventurous and generate an array of randomly shuffled numbers:

Applescript:


on shuffle(n)
   run script "[...Array(" & n & ").keys()].map((i)=>({at:Math.random(),value:i+1})).sort((x,y)=>x.at-y.at).map((x)=>x.value)" in "JavaScript"
end shuffle

log "shuffle(10): " & listToString(shuffle(10)) (*shuffle(10): 9, 6, 4, 1, 2, 5, 7, 3, 10, 8*)

Another +-1-liner! But the main point is that we can use just a single expression in the new JavaScript ES6 that now supports => arrow functions, the '...' spread operator and plenty of good array functions - such as keys(), map(), and again sort() - which can greatly simplify our AppleScreepy lifes. And thankfully JavaScript ES6 is now supported by AppleScript!

PS 1. Isn't it kinda funny that we use sorting to shuffle a list? (The algorithm is cool, it first creates an array without values, then fills it with the indices using '...' and key(). Using the first map() it then fills the array with objects with a random position and a value derived from each index. These objects can then be sorted according to their random position and then the second map() just extracts the values.)

PS 2. I have to admit that in order to put together shuffle() I had to steal the main components from two StackOverflow wizzards: Ben in Does JavaScript have a method like “range()” to generate a range within the supplied bounds? taught me to use '...' and key() and superluminary in How to randomize (shuffle) a JavaScript array? baffled me with the really ingenious way how to 'randomly sort' an array. (Note that this implementation is a bit slow not only because all the map() and key() calls traverse the entire array but more importantly because sort() can be the big O(n^2) deal while other shuffle algorithms need just O(n) - so use only for small arrays) hmm

Caveat:
Another interesting possibility is to use JavaScript's String functions that allow for RegExp (regular expressions) to simplify coding in AppleScript:

Applescript:


# Returns 'duality' surrounded by single quotes if it's a string, or untouched if it's a RegExp
on quotedIfString(duality)
   if first character of duality is "/" then return duality -- a RegExp, just return it
   return quoted form of duality -- it's a string, return it in quotes
end quotedIfString

on match(inString, regExp)
   run script "'" & inString & "'.match(" & regExp & ")" in "JavaScript"
end match

on replace(inString, duality, byString)
   run script "'" & inString & "'.replace(" & quotedIfString(duality) & ",'" & byString & "')" in "JavaScript"
end replace

on split(text_, duality)
   run script "'" & text_ & "'.split(" & quotedIfString(duality) & ")" in "JavaScript"
end split

on niceListString(list_)
   "[{" & replace(listToString(list_), "/, /g", "}, {") & "}]"
end niceListString

set niceList to niceListString(match("/Users/jan/.atom/styles.less", "/(.*\\/)(.+?)\\.(\\w+)$/"))

log "[{all, folder, name, extension}]: " & niceList
   (*{all, folder, name, extension}: [{/Users/jan/.atom/styles.less}, {/Users/jan/.atom/}, {styles}, {less}]*)
log "extension: " & item 5 of split(niceList, "/[\\[{, }\\]]{2,4}/") (*extension: less*)
log "extension: " & item 4 of split(replace(niceList, "/\\[{|}\\]/g", ""), "}, {") (*extension: less*)
log "extension: " & item 4 of (run script "'" & niceList & "'.replace('[{', '').replace('}]', '').split('}, {')" in "JavaScript") (*extension: less*)

Note 1: '\' in a RegExp must be doubled as '\\'.
Note 2: Obviously an 'extension' could be easily obtained as item 4 in the match(). But the contrived ways using the complicated niceList demonstrate nicely how useful are JavaScript's split() and replace() with and/or without a RegExp

...And of course how useful it can be to integrate JavaScript's features in AppleScript.

PS 3. Maybe this post could/should go to a separate category given it's generic and not just about Math?

Last edited by Honza (2021-06-15 06:59:08 pm)

Offline

 

#7 2021-06-16 12:03:51 pm

robertfern
Member
Registered: 2011-11-29
Posts: 112

Re: cos, sin, tan?

Here are some AppleScript routines to calculate Sine, Cosine, & Tangent
These are from a library I downloaded years ago. Not sure where.

Applescript:

on sine(x)
   local answer, numerator, denominator, factor
   if x ≥ 0 then
       set x to x mod 360
   else
       set x to 360 + (x mod 360)
   end if
   display alert x
   --convert from degrees to radians
   set x to x * (2 * pi) / 360
   
   set answer to 0
   set numerator to x
   set denominator to 1
   set factor to -(x ^ 2)
   
   repeat with i from 3 to 40 by 2
       set answer to answer + numerator / denominator
       set numerator to numerator * factor
       set denominator to denominator * i * (i - 1)
   end repeat
   
   return answer
end sine

on cosine(x)
   local answer, numerator, denominator, factor
   if x ≥ 0 then
       set x to x mod 360
   else
       set x to 360 + (x mod 360)
   end if
   
   --convert from degrees to radians
   set x to x * (2 * pi) / 360
   
   set answer to 0
   set numerator to 1
   set denominator to 1
   set factor to -(x ^ 2)
   
   repeat with i from 2 to 40 by 2
       set answer to answer + numerator / denominator
       set numerator to numerator * factor
       set denominator to denominator * i * (i - 1)
   end repeat
   
   return answer
end cosine

--x is in degrees
on tan(x)
   local answer
   set answer to sine(x) / (cosine(x))
   return answer
end tan

on tangent(x)
   local factor, sanswer, canswer, snum, cnum, sdenom, cdenom
   if x ≥ 0 then
       set x to x mod 360
   else
       set x to 360 + (x mod 360)
   end if
   
   --convert from degrees to radians
   set x to x * (2 * pi) / 360
   
   set {sanswer, canswer} to {0, 0}
   set {cnum, snum} to {1, x}
   set {cdenom, sdenom} to {1, 1}
   set factor to -(x ^ 2)
   
   repeat with i from 2 to 40 by 2
       set canswer to canswer + cnum / cdenom
       set sanswer to sanswer + snum / sdenom --(cdenom * (i + 1))
       set cnum to cnum * factor
       set snum to snum * factor
       set cdenom to cdenom * i * (i - 1)
       set sdenom to cdenom * (i + 1)
   end repeat
   
   return sanswer / canswer
end tangent

You'll notice there are 2 versions on Tangent

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)