In R
, the function outer
structurally allows you to take the outer product of two vectors x
and y
while providing a number of options for the actual function applied to each combination. For example outer(x,y,'-')
creates an "outer product" matrix of the elementwise differences between x
and y
. Does Julia have something similar?
In Julia, a function is an object that maps a tuple of argument values to a return value. Julia functions are not pure mathematical functions, because they can alter and be affected by the global state of the program. The basic syntax for defining functions in Julia is:
Outer constructor methods are defined outside of the block of type declaration. An outer constructor is just like any other function in Julia, its functionality depends on all the methods that are defined inside of it. A new functionality can be added to a constructor by simply defining a new method.
In Julia, most operators are just functions with support for special syntax. (The exceptions are operators with special evaluation semantics like && and ||. These operators cannot be functions since Short-Circuit Evaluation requires that their operands are not evaluated before evaluation of the operator.)
julia: outer product function. In R, the function outer structurally allows you to take the outer product of two vectors x and y while providing a number of options for the actual function applied to each combination. For example outer(x,y,'-') creates an "outer product" matrix of the elementwise differences between x and y.
Broadcast is the Julia operation which occurs when adding .
's around. When the two containers have the same size, it's an element-wise operation. Example: x.*y
is element-wise if size(x)==size(y)
. However, when the shapes don't match, then broadcast really comes into effect. If one of them is a row vector and one of them is a column vector, then the output will be 2D with out[i,j]
matching the i
th row of the column vector with the j
row vector. This means x .* y
is a peculiar way to write the outer product if one a row and the other is a column vector.
In general, what broadcast is doing is:
This is wasteful when dimensions get large, so Julia offers broadcast(), which expands singleton dimensions in array arguments to match the corresponding dimension in the other array without using extra memory
(This is from the Julia Manual)
But this generalizes to all of the other binary operators, so x .- y'
is what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With