Hyperbolic functions Sinh, Cosh, Tanh in Action Script 3 (AS3) - actionscript-3

I have been implementing a calculator in Action Script 3 and I found Math classes for sin, cos, tan, asin, acos, atan but I do not find any way to implement sinh, cosh, tanh, asinh, acosh, atanh in Action Script 3.
Do I need to write the code on the basis of raw formulas or is there any AS libraries available which do the work. I am not very good in math so do not want to write it using formulas. Also using formula may result in imprecise result.
Please suggest a way to figure it out for both the cases.

What formulas did you intend to use? With the means at hand you can easily define
cosh(x) = 0.5*(exp(x)+exp(-x))
sinh(x) = 0.5*(exp(x)-exp(-x))
tanh(x) = 1-2/(1+exp(2*x)) = (exp(2*x)-1)/(exp(2*x)+1)
acosh(x) = log(x+sqrt(x*x-1))
asinh(x) = log(x+sqrt(x*x+1))
atanh(x) = 0.5*log(2/(1-x)-1) = 0.5*(log(1+x)-log(1-x))

Related

Should I define a new function for making a matrix Hermitian?

Let X be a square matrix. We want to force it to be Hermitian, that is: self-conjugate-transpose. X = X^H = conj(X^T). To do this in Python with numpy is easy:
X = 0.5*(X + np.conj(X.T))
I haven't found in NumPy a single function that does it in a single experssion f(x).
The question is should I define a new function to do it? E.g.
def make_hermitian(X):
return 0.5*(X + np.conj(X.T))
(one can come up with short name, e.g. "make_h" or "herm" or "selfconj").
Pros: more readable code, one operation in shorter form. If one uses shorter name it saves writing when repeated many times, and makes modification in this operation far more easy and comfortable (need to change only in place).
Cons: replaces a very short and straight-forward expression which is self-evident.
What is more appropriate way of programming: define a new function or just write the explicit expression repeatedly?
I would say it depends on how many times you need to reuse that function.
If it's more than twice, then definitely make a function. If it's only once or twice, I would say it's up to you. If you choose to go with no function, add a short comment specifying what such piece of code is supposed to do.
My preference in any case would be defining a function with a meaningful name, because if anyone else is going to / supposed to read the code, they may not know or remember how to achieve a Hermitian matrix, and hence the math alone ain't going to be sufficient.
On the other hand, a meaningful function name will tell them clearly what it's going on, and they can google after what a Hermitian matrix is.

How to get cost/reward current estimation for all arms using Vowpal Wabbit with Python

I am starting to work with Vowpal Wabbit with Python and I am kinda struggling with its lack of documentation.
Do you guys know what modeling it uses as a cost/reward estimation for each arm? Do you know how to retrieve this current estimation?
vw = pyvw.vw("--cb_explore 2 --epsilon 0.2")
input = "2:-20:0.5 | Anna"
vw.learn(initial_input)
input = "1:-10:0.1 | Anna"
vw.learn(initial_input)
vw.predict(" | Anna")
Output would be:
[0.10000000149011612, 0.9000000357627869]
How can I also get the expected value for each arm? Something like
[-10.00, -20.00]
When using _explore you get back a PMF over the given actions. This is true for CB and CB_adf.
However, when using the non-explore version for each of those things differ a bit.
--cb is going to give you the chosen action directly, whereas --cb_adf is going to return the score for each given action.
So in this situation changing to using action dependent features (ADF) should provide the score/estimated cost.

Indexing the name of a function in a for loop in Julia

I'm trying to write a for loop to generate multiple functions. I would like to iterate on the name of the functions as well but I can't seem to make it work. Here's what I want to do:
for i = 1:n
h_[i](x) = i*x
end
I know this doesn't work but I would like something like that.
Thanks!
As #laborg mentioned, quite often you don't need/want to use metaprogramming. Since you already try to use array indexing syntax (h_[i]) perhaps you should simply create a vector of functions, for example like so:
h_ = [x->i*x for i in 1:n]
Here, x->i*x is an anonymous function - a function that we don't care to give a name. Afterwards you can use actual array indexing to access these different functions and call them.
Demo:
julia> n = 3;
julia> h_ = [x->i*x for i in 1:n];
julia> h_[1](3)
3
julia> h_[2](3)
6
julia> h_[3](3)
9
No metaprogramming involved.
(On a side note, in this particular example a single function h(x;i) = i*x with a keyword argument i would probably the best choice. But I assume this is a simplified example.)
You have to evaluate the name of your function:
for i = 1:n
f = Symbol(:h_,i)
#eval $f(x) = $i*x
end
More on this:
https://docs.julialang.org/en/v1/manual/metaprogramming/
As a general note on meta programming in Julia: It really helps to think twice if meta programming is the best solution, especially as Julia offers a lot of other cool features, e.g. multiple dispatch.

mlr support for xgb.create.features - how to use?

There is a function in the r mlr package that lists all the methods it supports for a given learner, which I have used once but cannot find again. I do recall that xgboost's xgb.create.feature was definitely on the included list, but I cannot find any docs on how to use it from within mlr. Does anyone know how to do this? (And if anyone can remember the name of the mlr search function for implemented learner methods that would also be much appreciated.)
xgb.create.features is a function from xgboost not mlr.
If you want to use the function, you can access the learner model directly and call the function.
library(mlr)
library(xgboost)
mod = train(makeLearner("classif.xgboost"), iris.task)
iris.dc = data.matrix(getTaskData(iris.task, target.extra = TRUE)$data)
xgboost::xgb.create.features(mod$learner.model, iris.dc)
Not all methods of learners are directly supported from mlr side.

Is there a function head in mathematica that can be used to define an input type?

I am defining a function that takes as input a function and I want to specify it in the input type i.e. Operat[_?FunctionQ]:=...
But there is no functionQ as of yet in mathematica. How do I get aroud this except not specifying any type at all.
Any ideas?
Oh!
This: Test if an expression is a Function?
may be the answer i am looking for. I am reading further
Is the solution proposed there robust?, i.e.:
FunctionQ[_Function | _InterpolatingFunction | _CompiledFunction] = True;
FunctionQ[f_Symbol] := Or[
DownValues[f] =!= {},
MemberQ[ Attributes[f], NumericFunction ]]
FunctionQ[_] = False;
The exhibited definition has great utility. The question is: what exactly constitutes a function in Mathematica? Pure functions and the like are easily to classify as functions, but what about definitions that involve pattern-matching? Consider:
h[g[x_]] ^:= x + 1
Is h to be considered a function? If so, it will be hard to identify as it will entail examining the up-values of every symbol in the system to make that determination. Is g a function? It has an up-value, but g[x] is an inert expression.
What about head composition:
f[x_][y_][z_] := x + y + z
Is f a function? How about f[1] or f[1][2]?
And then there are the various capabilities like JLink and NETLink:
Needs["JLink`"]
obj = JavaNew["java.util.Date"]
obj#toString[]
Is obj#toString a function?
I hate to bring up these problems without offering solutions -- but I want to emphasize that the question as to what constitutes a function in the Mathematica context is a tricky one. It is tricky from both the theoretical and practical standpoints.
I think that the answer to whether the exhibited function test is complete really depends upon the types of expressions that you will be feeding it in your specific application.