arrays and plotting plt plot( ,yplt show( variation of the same approach is to alter the (xfunction itself and put the for loop inside itdef h_loop( ) np zeros(len( )or copy(for in range(len( )) [ih( [ ]return np linspace(- + h_loop(xwe see that this last approach ensures that we can call the function with an array argument xbut the downside to both versions is that we need to write quite lot of new codeand using for loop is much slower than using vectorized array computing an alternative approach is to use built-sin numpy function named vectorize which offers automatic vectorization of functions with if-tests the line hv np vectorize(hcreates vectorized version hv(xof the function (xthat will work with an array argument although this approach is obviously betterin the sense that the conversion is automatic so we need to write very little new codeit is about as slow as the two approaches using for loops third approach is to write new function where the if-test is coded differentlydef hv( )return np where( for this particular casethe numpy function where will evaluate the expression < for all elements in the array xand return an array of the same length as xwith values for all elements where < and for the others more generallya function with an if-test can be converted to an array-ready vectorized version in the following waydef ( ) it is fairly common misconception to believe that vectorizing computation or making vectorized version of functionalways involves using the function numpy vectorize this is not the case in most caseswe only need to make sure that we use array-ready functionssuch as numpy sinnumpy expetc instead of the scalar version from mathand code all calculations so that they work on an entire array instead of stepping through the elements with for loop the vectorize-function is usually only necessary for functions containing if-tests