Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

julia: outer product function

Tags:

r

julia

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?

like image 572
jjjjjj Avatar asked Jun 16 '17 14:06

jjjjjj


People also ask

What is a function in Julia?

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:

What is an outer constructor in Julia?

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.

What is an operator in Julia?

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.)

How do you use the outer product function in R?

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.


1 Answers

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 ith 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.

like image 131
Chris Rackauckas Avatar answered Oct 27 '22 02:10

Chris Rackauckas