@@ -355,10 +355,10 @@ Exercises 4
355355
356356 - **In-place addition ** (advanced): Create an array of
357357 ``dtype='float' ``, and an array of ``dtype='int' ``. Try to use the
358- int array is the output argument of the first two arrays.
358+ int array as the output argument of the first two arrays.
359359
360360 - **Output arguments and timing ** Repeat the initial ``b = a **
361- 2 `` example using the output arguments and time it. Can you make
361+ 2 `` example using a ufunc and time it. Can you make
362362 it even faster using the output argument?
363363
364364.. solution :: Solution: Numpy-4
@@ -367,22 +367,21 @@ Exercises 4
367367
368368 x = np.array([1, 2, 3])
369369 id(x) # get the memory-ID of x
370- np.add(x, x, x) # Third argument is output array
371- np.add(x, x, x)
370+ np.add(x, x, out=x) # Third argument is output array
371+ np.add(x, x, out= x)
372372 print(x)
373373 id(x) # get the memory-ID of x
374374 # - notice it is the same
375375
376- You note that ``np.add()`` has a third argument that is the
377- output array (same as ``out=``), *and* the function returns that
378- same array.
376+ Note that ``np.add()`` writes the result to the output array (``out=``)
377+ *and* the function returns that same array.
379378
380379
381380 - **Output arguments and timing ** In this case, on my computer, it was
382381 actually slower (this is due to it being such a small array!)::
383382
384- a = np.arange(10000 )
385- b = np.zeros(10000 )
383+ a = np.arange(10_000 )
384+ b = np.zeros(10_000 )
386385
387386 ::
388387
@@ -392,6 +391,11 @@ Exercises 4
392391 This is a good example of why you always need to time things
393392 before deciding what is best.
394393
394+ Note: the ``_ `` inside numbers is just for human readability and is
395+ ignored by python.
396+
397+
398+
395399
396400Linear algebra and other advanced math
397401--------------------------------------
0 commit comments