Raising an array by a power

1,565 views
Skip to first unread message

yaois...@gmail.com

unread,
Jul 14, 2014, 5:26:38 PM7/14/14
to julia...@googlegroups.com
Hi Julia users,
I am absolutely embarrassed to ask this question, but I have not been able to find the correct combination of query words to yield the desired answer in the documentation or Google. Anyhow, what function could I use to raise an array to a power?

testarray = Int64[1,2,3,4]
pow(testarray,2)

or

power(testarray,2)

do not work.

Thank you for your help.

Regards,
WX

Elliot Saba

unread,
Jul 14, 2014, 5:39:52 PM7/14/14
to julia...@googlegroups.com
If what you want is to raise each individual element to that power, (e.g. to result in Int64[1,4,9,16]) then what you want is the .^ operator:

julia> [1,2,3,4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> [1,2,3,4].^2
4-element Array{Int64,1}:
  1
  4
  9
 16

In general, element-by-element operators are prefixed by "." so as to disambiguate them from the more general linear algebraic operators that expect to treat arrays as mathematical objects in their own right, rather than collections of mathematical objects.
-E

Stefan Karpinski

unread,
Jul 14, 2014, 5:40:36 PM7/14/14
to Julia Users
No problem, this is a very difficult bit of punctuation:

julia> testarray.^2
4-element Array{Int64,1}:
  1
  4
  9
 16

This is easy for Matlab users to guess but probably not for others.

Stefan Karpinski

unread,
Jul 14, 2014, 5:45:16 PM7/14/14
to Julia Users
On Mon, Jul 14, 2014 at 2:39 PM, Stefan Karpinski <ste...@karpinski.org> wrote:
very difficult bit of punctuation

Incomplete sentence: very difficult bit of punctuation to Google.

yaois...@gmail.com

unread,
Jul 14, 2014, 6:07:58 PM7/14/14
to julia...@googlegroups.com
Thank you!

Alan Edelman

unread,
Jul 15, 2014, 9:13:57 AM7/15/14
to julia...@googlegroups.com
If you read dot as "point"
Then pointwise ops make a kind of sense
Reply all
Reply to author
Forward
0 new messages