diff --git a/README.html b/README.html index 7cdb9f4..76e812e 100644 --- a/README.html +++ b/README.html @@ -164,6 +164,18 @@ python -c "import numpy; numpy.info(num print(Z) +
  • Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) +.. code-block:: python

    +
    +

    # Author: Evgeni Burovski

    +

    Z = np.arange(11) +Z[(3 < Z) & (Z <= 8)] *= -1

    +
    +

    System Message: WARNING/2 (README.rst, line 209); backlink

    +

    Inline emphasis start-string without end-string.

    +
    +
    +
  • Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)

     Z = np.zeros((5,5))
    @@ -187,13 +199,21 @@ array (★☆☆)

    print(Z)
  • -
  • Create a random vector of size 10 and sort it (★★☆)

    +
  • Create a random vector of size 10 and sort it (★★☆)

     Z = np.random.random(10)
     Z.sort()
     print(Z)
     
  • +
  • How to sum a small array faster than np.sum ? (★★☆)

    +
    +# Author: Evgeni Burovski
    +
    +Z = np.arange(10)
    +np.add.reduce(Z)
    +
    +
  • Consider two random array A anb B, check if they are equal (★★☆)

     A = np.random.randint(0,2,5)
    @@ -236,6 +256,16 @@ them to polar coordinates (★★☆)

    print(Z)
  • +
  • Given two arrays, X and Y, construct the Cauchy matrix C (Cij = 1/(xi - yj))

    +
    +# Author: Evgeni Burovski
    +
    +X = np.arange(8)
    +Y = X + 0.5
    +C = 1.0 / np.subtract.outer(X, Y)
    +print(np.linalg.det(C))
    +
    +
  • Print the minimum and maximum representable value for each numpy scalar type (★★☆)

     for dtype in [np.int8, np.int32, np.int64]:
    @@ -254,13 +284,6 @@ them to polar coordinates (★★☆)

    print(Z)
  • -
  • How to print all the values of an array ? (★★☆)

    -
    -np.set_printoptions(threshold=np.nan)
    -Z = np.zeros((25,25))
    -print(Z)
    -
    -
  • How to find the closest value (to a given scalar) in an array ? (★★☆)

     Z = np.arange(100)
    @@ -706,7 +729,6 @@ How to compute the sum of of the p matrix products at once ? (result has shape (
     
  • How to get the n largest values of an array (★★★)

    -
     Z = np.arange(10000)
     np.random.shuffle(Z)
    @@ -718,7 +740,6 @@ How to compute the sum of of the p matrix products at once ? (result has shape (
     # Fast
     print (Z[np.argpartition(-Z,n)[:n]])
     
    -
  • Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)

    @@ -846,6 +867,21 @@ equidistant samples (★★★) ?

    y_int = np.interp(r_int, r, y)
  • +
  • Given an integer n and a 2D array X, select from X the rows which can be +interpreted as draws from a multinomial distribution with n degrees, i.e., +the rows which only contain integers and which sum to n. (★★★)

    +
    +# Author: Evgeni Burovski
    +
    +X = np.asarray([[1.0, 0.0, 3.0, 8.0],
    +                [2.0, 0.0, 1.0, 1.0],
    +                [1.5, 2.5, 1.0, 0.0]])
    +n = 4
    +M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)
    +M &= (X.sum(axis=-1) == n)
    +print(X[M])
    +
    +
  • diff --git a/README.rst b/README.rst index 1c20f57..fb49a01 100644 --- a/README.rst +++ b/README.rst @@ -201,6 +201,14 @@ Thanks to Michiaki Ariga, there is now a Z = np.dot(np.ones((5,3)), np.ones((3,2))) print(Z) +#. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) + .. code-block:: python + + # Author: Evgeni Burovski + + Z = np.arange(11) + Z[(3 < Z) & (Z <= 8)] *= -1 + #. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) @@ -231,7 +239,7 @@ Thanks to Michiaki Ariga, there is now a print(Z) -#. Create a random vector of size 10 and sort it (★★☆) +#. Create a random vector of size 10 and sort it (★★☆) .. code-block:: python @@ -240,6 +248,16 @@ Thanks to Michiaki Ariga, there is now a print(Z) +#. How to sum a small array faster than np.sum ? (★★☆) + + .. code-block:: python + + # Author: Evgeni Burovski + + Z = np.arange(10) + np.add.reduce(Z) + + #. Consider two random array A anb B, check if they are equal (★★☆) .. code-block:: python @@ -293,6 +311,18 @@ Thanks to Michiaki Ariga, there is now a print(Z) +#. Given two arrays, X and Y, construct the Cauchy matrix C (Cij = 1/(xi - yj)) + + .. code-block:: python + + # Author: Evgeni Burovski + + X = np.arange(8) + Y = X + 0.5 + C = 1.0 / np.subtract.outer(X, Y) + print(np.linalg.det(C)) + + #. Print the minimum and maximum representable value for each numpy scalar type (★★☆) .. code-block:: python @@ -919,6 +949,7 @@ Thanks to Michiaki Ariga, there is now a print(Z) print(U) + #. Convert a vector of ints into a matrix binary representation (★★★) @@ -981,3 +1012,20 @@ Thanks to Michiaki Ariga, there is now a r_int = np.linspace(0, r.max(), 200) # regular spaced path x_int = np.interp(r_int, r, x) # integrate path y_int = np.interp(r_int, r, y) + + +#. Given an integer n and a 2D array X, select from X the rows which can be + interpreted as draws from a multinomial distribution with n degrees, i.e., + the rows which only contain integers and which sum to n. (★★★) + + .. code-block:: python + + # Author: Evgeni Burovski + + X = np.asarray([[1.0, 0.0, 3.0, 8.0], + [2.0, 0.0, 1.0, 1.0], + [1.5, 2.5, 1.0, 0.0]]) + n = 4 + M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1) + M &= (X.sum(axis=-1) == n) + print(X[M])