I can vectorize a function using the dot notation:
a = Vector(0:10) .* 4
As in plenty of examples I read the dot comes prior to the asterisk.
However, this does not work in the following case:
Complex.(a,a)
Here the dot suddenly goes behind the function name.
Is this intended? And is there a rule?
For functions, the dot always goes behind the function name.
For operators, like * or + for example, the dot goes before the operator. However, you can enclose the operator in parentheses and suffix the dot.
To make this difference even more explicit, consider this example where we apply "multiply" with function call syntax:
x = rand(2,2)
sqrt.(x)
.*(x,x)
(*).(x,x)
x .* x
The last three commands all do the same thing.
See the corresponding sections of the Julia documentation for more: Dot Syntax for Vectorizing Functions and Vectorized "dot" operators.
Related
[ and [[ come up a lot when using R. Suppose that I'm having a conversation about these two functions, what do I actually call these "indexing operators"? I know how to name them as punctuation, but is there anything within R or its documentation that gives them a more specific name? I know that they're subsetting functions that are documented under ?Extract, but I've never seen anyone call them anything like "extract and double extract".
In the R Language Definition they are referred to as "single and double brackets":
Indexing of arrays and vectors is performed using the single and double brackets, [] and [[]]
In An Introduction to R you find several instances of "square bracket" and "double square bracket".
This agrees with the general terminology (see e.g. wikipedia on square brackets and double brackets, and their Unicode).
As an example of what I'm after, suppose that I want every multiple of 3 in the list of numbers from 1 to 100. The easier way that I know of doing this is as follows:
all<-1:100
mod3<-all[all%%3==0]
Is there a way to do this without the first line? It seems silly that I have to declare a list (and therefore give it a name) before I can subset it. I could call the subset function, but the array notation in my second line comes so naturally that it would be a shame to not use it. In an ideal world, I'd call something like 1:100[1:100%%3==0] but that obviously gives errors. Referring only to things that are built-in to R, what are my options?
We could wrap within brackets to make it a self-contained block as there is precedence for different operators
(1:100)[(1:100)%%3==0]
If the check the ?Syntax, the different operator precedence are given
#:: ::: access variables in a namespace
#$ # component / slot extraction
#[ [[ indexing
#^ exponentiation (right to left)
#- + unary minus and plus
#: sequence operator
#%any% special operators (including %% and %/%)
#* / multiply, divide
#...
In the above case, the expression within the [] works as expected because the : is above on the precedence wrt to %%. But, it is the one outside having an issue because [ is higher when compared to :
I read in several places that pipes in Julia only work with functions that take only one argument. This is not true, since I can do the following:
function power(a, b = 2) a^b end
3 |> power
> 9
and it works fine.
However, I but can't completely get my head around the pipe. E.g. why is this not working?? :
3 |> power()
> MethodError: no method matching power()
What I would actually like to do is using a pipe and define additional arguments, e.g. keyword arguments so that it is actually clear which argument to pass when piping (namely the only positional one):
function power(a; b = 2) a^b end
3 |> power(b = 3)
Is there any way to do something like this?
I know I could do a work-around with the Pipe package, but to honest it feels kind of clunky to write #pipe at the start of half of the lines.
In R the magritrr package has convincing logic (in my opinion): it passes what's left of the pipe by default as the first argument to the function on the right - I'm looking for something similar.
power as defined in the first snippet has two methods. One with one argument, one with two. So the point about |> working only with one-argument methods still holds.
The kind of thing you want to do is called "partial application", and very common in functional languages. You can always write
3 |> (a -> power(a, 3))
but that gets clunky quickly. Other language have syntax like power(%1, 3) to denote that lambda. There's discussion to add something similar to Julia, but it's difficult to get right. Pipe is exactly the macro-based fix for it.
If you have control over the defined method, you can also implement methods with an interface that return partially applied versions as you like -- many predicates in Base do this already, e.g., ==(1). There's also the option of Base.Fix2(power, 3), but that's not really an improvement, if you ask me (apart from maybe being nicer to the compiler).
And note that magrittrs pipes are also "macro"-based. The difference is that argument passing in R is way more complicated, and you can't see from outside whether an argument is used as a value or as an expression (essentially, R passes a thunk containing the expression and a pointer to the parent environment, and automatically evaluates and caches it if you use it as a value; see substitute)
In Julia we can redefine the operator like +,× ,sin, cos and so on. But one interesting thing is that after the redefinition the order among all those amended operators still hold,like multiplication still goes before summation no matter how you redefine your own function,why?
This is simply a function of how operator precedence is defined for infix operators. You can find the precedence table in the manual. Precedence defines how Julia code is parsed into a sequence of nested function calls. You can manually specify precedence with parentheses or traditional function call syntax.
You can see this happening just by asking Julia to parse (but not execute) the code. It returns back a quoted expression, which is printed in a way that mimics the standard way of writing that code in Julia. There are a number of tools that allow you to inspect this quoted expression; one that's helpful to display the nesting of calls is Meta.show_sexpr.
julia> ex = parse("1 + 2 * 3")
:(1 + 2 * 3)
julia> Meta.show_sexpr(ex)
(:call, :+, 1, (:call, :*, 2, 3))
This is simply stating that it will multiply 2*3 before adding 1 to it. That is entirely based upon the nested structure of the parentheses. Whatever function is bound to * will get executed first, regardless of its definition. Now compare the results above with things like:
parse("(1 + 2) * 3")
parse("+(1, *(2, 3))")
parse("*(1, +(2, 3))")
I like solving my math problems(high school) using R as it is faster than writing on a piece of paper. One problem I'm having is that I have to keep writing the multiplication sign, example:
9x^2 + 24x + 16 yields = Error: unexpected symbol in "9x"
Is there any way in R to multiply 4x, without having to write 4*x but only 4x?
Would save me some time in having to write one extra character the whole time! Thanks
No. Having a number in front of a character without any space simply isn't valid syntax in R.
Take a step back and look at the syntax rules for, say, Excel, Matlab, Python, Mathematica. Every language has its rules, generally (:-) ) with good reason. For example, in R, the following are legal object names:
foo
foo.bar
foo1
foo39
But 39foo is not legal. So if you wanted any sequence [0-9][Letters] or the reverse to indicate multiplication, you'd have a conflict with naming rules.