User specified function with operators in R - r

I want to use a user-specified function and apply the function to a list of values. I envision that the user will give a 'formula' as a character string containing the names of variable and operators, e.g. "a * b %% c - d / e ^ f + g %/% h".
The following toy example works
prmlist <- list(a=1:10, b=21:30, c=31:40, d=4, e=5, f=6, g=7, h=8)
with(prmlist, a * b %% c - d / e ^ f + g %/% h)
The problem starts when I want to use this approach within a function. To do that I must get the 'formula' specified by the user inside the function. A character string seems the obvious route. The question is how to evaluate it inside the function. do.call() doesn't seem to be suited because the operators are each really a function. I hoped something simple like
my.formula <- "a * b %% c - d / e ^ f + g %/% h"
with(prmlist, eval(my.formula))
would work but it doesn't.

You can envoke your operators using substitute() instead:
my.formula <- substitute(a * b %% c - d / e ^ f + g %/% h)
with(prmlist, eval(my.formula))
[1] 20.99974 43.99974 68.99974 95.99974 124.99974 155.99974 188.99974
[8] 223.99974 260.99974 299.99974
Update: If the command is a string you can use parse:
myCmd <- "a * b %% c - d / e ^ f + g %/% h"
with(prmlist, eval( parse(text=myCmd) ))
[1] 20.99974 43.99974 68.99974 95.99974 124.99974 155.99974 188.99974
[8] 223.99974 260.99974 299.99974

Related

R writing functions

I want to write a mathematical formula as functions in R.
My formula V(C) = 1/1 + (C / H) ^ n where C, H and n are the parameter of the function that I can change. This is my first attempts to write a function in R, so please help.
V <- function( C, H, n ){
1 / 1 + ( C / H )^n
}

In Julia, Transpose operator

I am trying to use a transpose operator over a vector in order to perform am element-wise addition.
For example, I want to add a column vector a = [a1;a2;a3] to a row vector b = [b1,b2] I should get matrix
M = a+b = [a1+b1, a1+b2; a2+b1, a2+b2; a3+b1, a3+b2].
In MATLAB it is equivalent (if both vectors are row vectors) M = a.'+b
I am trying to get the same in Julia but here is the problem, there is no .' operator in Julia starting from 1.0 version. There is the transpose operator which does not work in broadcasting mode. The adjoint operator is not Valid for me because I work with complex numbers.
a = Vector{ComplexF64}([1+3im,2])
b = Vector{ComplexF64}([0,0,0])
Z = zeros(ComplexF64,3,2)
G = zeros(ComplexF64,3,2)
#. Z = b + a' # Works but takes the complex conjugate
#. Z = b + transpose(a) # DOES NOT WORK!!!! The error is " DimensionMismatch("array could not be broadcast to match destination") "
Z = b .+ transpose(a) # Works but not efficient
#. Z = b + conj(a')
The third case Z = b .+ transpose(a) is not efficient because it makes 2 loops first one for addition b .+ transpose(a), than it runs the second loop one for the assignment of b .+ transpose(a) to Z. While the other 3 cases do it within one loop.
So which is the fastest way?
And why transpose doesn't within Broadcasting?
Thank you in advance
For Hermitian You can just type:
a' .+ b
Example
julia> a = ComplexF64.([1+3im,2])
2-element Array{Complex{Float64},1}:
1.0 + 3.0im
2.0 + 0.0im
julia> b = ComplexF64.([10,20,30])
3-element Array{Complex{Float64},1}:
10.0 + 0.0im
20.0 + 0.0im
30.0 + 0.0im
julia> a' .+ b
3×2 Array{Complex{Float64},2}:
11.0-3.0im 12.0+0.0im
21.0-3.0im 22.0+0.0im
31.0-3.0im 32.0+0.0im
If you want to have just transposition you could define your own unary operator (perhaps from the list of unused unary operators):
¬(a) = permutedims(a)
Now you could do
julia> ¬a
1×2 Matrix{ComplexF64}:
1.0+3.0im 2.0+0.0im

Simplifying 5-var Boolean SOP Expression using the Laws and Properties

I have this question that is messing me up because I am not getting that where should I start, which terms should I pick at the beginning? Because this confusing expression does not even let me take the common as it makes no sense. Also, it does not even let me remove compliments (using the Laws) as it also does not make any sense. Please help me in this, at least just guide me what should I do? From where should I start? I would be really grateful.
The Explanation of Symbols I used to write the expression:
! : NOT Gate
+ : OR Gate
. (dot) : AND Gate
Boolean Expression:
A.!B.E + !(B.C).D.!E + !(C.D).E+!A.D.!E + A.!(C.D).E + A.E + A.B.!E + !(A.C) + B.C.!D
I have used an online expression simplifier and that gave me the following answer:
!A + B + !C + D + E
But how the above long expression has been simplified in this short one? I know the Laws and Properties but I am not getting that how should I start simplifying the long one? Which terms should I see first? Kindly anyone please help me.
(This is a direct answer to your comment, and a side-ways answer to your main question. In short, use a different method to get the desired simplified expression.)
You have a complicated expression, but one that uses only 5 logical variables. In this problem it would be much easier to build a truth table, which would have just 2^5 = 32 rows. You could look at the results and use those to build a simplified, equivalent expression. This does not use "the laws and properties" that your original question requires, but it is a standard technique to simplify Boolean expressions.
You should have learned how to build a truth table in just about any Discrete Mathematics class. In short, you make a table where each element in each row is a T for True or F for False. The rows contain all possible combinations of Ts and Fs. For 5 variables this would use 2^5 = 32 rows. For each row, you assign the first value to A, the second to B, etc. You then evaluate the expression for those values and write the result at the end of the line.
This can be done by hand, but your expression is complicated enough that we could avoid that. Here is a Python 3 script that prints the desired table. Note that Python has the product() function which simplifies getting all possible combinations of Ts and Fs. This script used B[] to convert a Boolean value to a single character T or F.
from itertools import product
"""Make a truth table for the Boolean expression
A.!B.E + !(B.C).D.!E + !(C.D).E+!A.D.!E + A.!(C.D).E + A.E + A.B.!E + !(A.C) + B.C.!D
"""
B = ('F', 'T')
print('A B C D E : Result')
print('- - - - - : ------')
for a, b, c, d, e in product((True, False), repeat=5):
print(B[a], B[b], B[c], B[d], B[e], end=' : ')
print(B[
(a and not b and e)
or (not (b and c) and d and not e)
or (not (c and d) and e)
or (not a and d and not e)
or (a and not (c and d) and e)
or (a and e)
or (a and b and not e)
or (not (a and c))
or (b and c and not d)
])
Here are the results:
A B C D E : Result
- - - - - : ------
T T T T T : T
T T T T F : T
T T T F T : T
T T T F F : T
T T F T T : T
T T F T F : T
T T F F T : T
T T F F F : T
T F T T T : T
T F T T F : T
T F T F T : T
T F T F F : F
T F F T T : T
T F F T F : T
T F F F T : T
T F F F F : T
F T T T T : T
F T T T F : T
F T T F T : T
F T T F F : T
F T F T T : T
F T F T F : T
F T F F T : T
F T F F F : T
F F T T T : T
F F T T F : T
F F T F T : T
F F T F F : T
F F F T T : T
F F F T F : T
F F F F T : T
F F F F F : T
We see that the result is always T except for the single line T F T F F. This means your expression is true unless A is True, B is False, C is True, and D and E are False. So we can simplify your expression (using your notation) to
!(A.!B.C.!D.!E)
A simple use of DeMorgan's laws changes this to normal form:
!A + B + !C + D + E
which is what you wanted.

Is there a infinite loop in my codes? in ocaml

I want to get the sum of function f(i) values when i is equal from a to b
= f(a)+f(a+1)+...+f(b-1)+f(b)
So I wrote code like this.
let rec sigma : (int -> int) -> int -> int -> int
= fun f a b ->
if a=b then f a
else f b + sigma f a b-1 ;;
but result is that there is stack overflow during evaluation. Is there a infinite loop? and why?
sigma f a b-1 is parsed as (sigma f a b) - 1 instead of your intention, sigma f a (b-1). Since sigma f a b calls sigma f a b recursively in your code, it never stops.
The best practice is to put white spaces around binary operators like sigma f a b - 1 so that you would not misread what you write.

How to express variable through other in SAGE

Is there way to express mathematical expression through variable defined earlier in SAGE?
For example if I have variable a = b + c, I want SAGE rewrite expression b + c + d as a + d.
Thank you.
In fact, substituting such expressions is a nontrivial thing if you don't know what part of the expression tree you want. See Richard Fateman's comments here.
The core of the problem is that even the command that would do what you want is not about strings, but expressions.
sage: var("a b c d")
(a, b, c, d)
sage: (a+d).subs({a:b+c})
b + c + d
sage: (b+c+d).subs({b+c:a})
b + c + d
So you will have to use a "wildcard".
sage: w0 = SR.wild(0)
sage: (b+c+d).subs({b+c+w0:a+w0})
a + d
For more information, see
sage: x.match?
sage: SR.wild?
in the interactive shell or notebook.
As you can see in calculus, you can express d as variable with
a = var('a'); b+c
or like a function of b and c variable

Resources