2

I need to translate these two lines to mathematica from python

 gamma = np.array([u/np.linalg.norm(u) for u in x[:y]])
 beta = np.array([u/np.linalg.norm(u) for u in x[y:]])

where y is a number.

abc
  • 31
  • 1
  • 5
    I guess ":" translates to ";;", a.k.a. Span. – Henrik Schumacher Mar 24 '21 at 13:10
  • 6
    There are two off-by-one bugs possible here: Python a[x:y] means taking elements x to y-1, where the first element is element zero. Mathematica a[[x;;y]] means taking elements x to y, where the first element is element 1. So Python a[x:y] should be written as a[[x+1;;y]] in Mathematica. – Roman Mar 24 '21 at 13:14

1 Answers1

8

: in python denotes ranges of array elements, and is equivalent to Span (;;) as noted by Henrik Schumacher's comment above

Note that Mathematica indexes from 1, whereas Python indexes from zero; additionally the array range specification in Python has the last term as an "up to but not including", whereas in Mathematica, it is inclusive of that element (as noted in Roman's comment above)

As best I understand your problem, it looks something like:

gamma = Norm/@x[[;;y-1]]
beta = Norm/@x[[y;;]]

But check your array indexing convention to make sure that the upper and lower bounds are what you intend them to be.

Joshua Schrier
  • 3,356
  • 8
  • 19