text:
Numpy Crash Course for Machine Learning

Learn the minimum numpy needed to get started with machine learning (1h course)

Die-hard C++ or Fortran users among physicists often say that python is too slow.

True, python is an interpreted language and it is slow.

Even python advocates like me realize it, but we think that the (lack of) speed of python is not really an issue, e.g. because:

• The time spent computing is balanced by a much smaller development time;
• Profiling is easy, which means that one can find the parts of the code that are slow, optimize them, and even write them in faster languages so that they can be compiled and used from python;

And most importantly:

• Some python tools like numpy are as fast as plain C.

In this tutorial, you will understand what is numpy and why it's fast, and learn just what's needed about numpy for the usual machine learning operations.

At the end, you will work on practical, real-life numpy operations, typically encountered in machine-learning projects.

This tutorial is interactive.

To run it on Google Colab click on this link. You'll be able to execute the code on your own, and to modify it.

Prerequisites: Please follow Python Crash Course for Machine Learning first, or be sure to master the basics of python.

The numpy array : Why is it fast?¶

The main purpose of numpy is to provide a very efficient data structure called the numpy array, and the tools to manipulate such arrays.

Why is the numpy array so fast?

Because, under the hood, arrays are processed with compiled code, optimized for the CPU. In particular, numpy operations are parallel as they use SIMD (Single Operation Multiple Data).

To see how fast numpy is, we can time it.

Let us create a large list with one million integers, and a numpy array from this list:

In [5]:
import numpy as np
lst = range(1000000)
arr = np.array(lst)
arr

Out[5]:
array([     0,      1,      2, ..., 999997, 999998, 999999])

Now let's compute the square of all integers, and see how much time it takes.

We start by the list:

In [6]:
%timeit squares = [x**2 for x in lst]

1 loop, best of 5: 299 ms per loop

And we do the same for the array:

In [7]:
%timeit squares = arr**2

100 loops, best of 5: 2.02 ms per loop

As you can see, this is 300 times faster.

We can in principle loop on the numpy array like this:

In [8]:
%timeit squares = [x**2 for x in arr]

1 loop, best of 5: 354 ms per loop

But then, we completely lose the benefits of numpy! Indeed, when we do arr**2 we use the square function of numpy, which is intrinsically parallel. When we loop, we process the elements one by one with basic python. So:

Never ever loop on a numpy array! You'll be tempted to do so, but there should be no exception!

Numpy array data types¶

We have seen that numpy arrays are processed by compiled code with SIMD.

For this to work, the elements in a numpy array must be:

• of a basic type, e.g. integers or floats. The full range of possibilities is given on this page;
• of the same type, so that they have the same size, e.g. 64 bits floats, or 16 bits integers.

On the contrary, python lists can contain heterogeneous objects of any type.

Here are a few ways to create numpy arrays with different types:

In [9]:
from sys import getsizeof as sizeof

# numpy guesses that it should use integers
x = np.array([0, 1, 2])
print(x.dtype)
# or floats:
x = np.array([0., 1., 2.])
print(x.dtype)
# here we specify a python compatible type,
# interpreted by numpy as int64
x = np.array([0., 1., 2.], dtype=int)
print(x.dtype)
# here we specify that we want 8 bits integers
x = np.array([0, 1, 2], dtype=np.int8)
print(x.dtype)

int64
float64
int64
int8

This makes it easy to estimate the size of a numpy array in memory, to see if you're going to blow up your computer before you actually do.

Exercise

For example, let's consider a sample of 1000 images, each with 200x200 pixels, and 3 color channels per pixel. The color index ranges from 0 to 255, and can thus be encoded as an 8 bit integer.

Assuming you store the data of all images in a single numpy array, what would be its size in memory in GB?

Numpy element wise operations¶

I call element wise operations all the operations that affect the array elements, but preserve the array shape.

All the usual operators are implemented in numpy, for arrays. For example:

In [10]:
x = np.array(range(5))
x**2

Out[10]:
array([ 0,  1,  4,  9, 16])

Note that these operators, in numpy, are element wise:

In [11]:
x+1

Out[11]:
array([1, 2, 3, 4, 5])

The element-wise equivalents of the functions in the python math package are available directly from the numpy package, with the same name, e.g.:

In [12]:
np.exp(x)

Out[12]:
array([ 1.        ,  2.71828183,  7.3890561 , 20.08553692, 54.59815003])

Finally, binary operators are available:

In [13]:
x = np.array([0, 1, 2])
y = np.array([1, 2, 3])
x+y

Out[13]:
array([1, 3, 5])
In [14]:
x*y

Out[14]:
array([0, 2, 6])

Numpy array shape¶

So far, we have only seen arrays with a single dimension. But often, more dimensions are used.

Multidimensional arrays can be created from a list of lists, e.g.:

In [15]:
x = np.array([[0, 1], [2, 3], [4,5]])
x

Out[15]:
array([[0, 1],
[2, 3],
[4, 5]])

The shape attribute gives us the length of each dimension:

In [16]:
x.shape

Out[16]:
(3, 2)

In this case, we have 3 rows of 2 numbers. The first dimension is the outermost dimension, and the second one the innermost dimension.

Let's take the example of a 2x2 pixels image, with 3 color channels (red, blue, green) in each pixel:

In [17]:
x = np.array(
[
[ [1,2,3], [4,5,6], ],
[ [7,8,9], [10,11,12]]
]
)
print(x)
print(x.shape)

[[[ 1  2  3]
[ 4  5  6]]

[[ 7  8  9]
[10 11 12]]]
(2, 2, 3)

To visualize numpy arrays more easily, I often think of the innermost dimension separately. For example, here, we have a 2x2 pixel array, with a sub-array of size 3 in each pixel.

And as a final example, let's consider a "column vector":

In [18]:
x = np.array([
[0],
[1],
[2],
[3]
])
print(x)
print(x.shape)

[[0]
[1]
[2]
[3]]
(4, 1)

As you can see, the "column vector" has two dimensions, which may be counterintuitive. There is a single number (a scalar) on the innermost dimension.

Please note that in numpy, a dimension can also be called an "axis".

Very often, numpy arrays of a given shape are built by initializing all elements to a fixed number, or a random number. For example:

In [19]:
np.zeros((2,3))

Out[19]:
array([[0., 0., 0.],
[0., 0., 0.]])
In [20]:
np.ones(3)

Out[20]:
array([1., 1., 1.])
In [21]:
np.ones_like(x)

Out[21]:
array([[1],
[1],
[1],
[1]])
In [22]:
np.random.rand(2,2)

Out[22]:
array([[0.34980331, 0.81149696],
[0.11857687, 0.69406662]])

Many more random sampling tools are available.

Numpy array indexing¶

Basic indexing¶

Here is a 1D array:

In [23]:
x = np.arange(10) + 1
x

Out[23]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Elements can be accessed directly, using their index in the array (the index starts at 0):

In [24]:
x[1]

Out[24]:
2

And, as usual in python sequences, negative arrays start from the end:

In [25]:
x[-2]

Out[25]:
9

The array can be modified in place:

In [26]:
print(id(x))
x[1] = 0
print(id(x))
print(x)

139874504620272
139874504620272
[ 1  0  3  4  5  6  7  8  9 10]

For multidimensional arrays, basic indexing is done by specifying a comma separated list of indices:

In [27]:
x = np.zeros((2,3))
x[0,1] = 1
x

Out[27]:
array([[0., 1., 0.],
[0., 0., 0.]])

Selection with boolean indexing¶

Indexing can be used to select array elements according to a mask.

Let's create our 1D array again:

In [28]:
x = np.arange(10) + 1
x

Out[28]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

To create a mask, we evaluate a boolean expression for each element in the array. For example, to find all even numbers:

In [29]:
x%2 == 0

Out[29]:
array([False,  True, False,  True, False,  True, False,  True, False,
True])

What does this expression mean?

Since x is a numpy array, x%2 is a numpy element-wise operation that evaluates %2 on all elements of the array, and returns a new array with the results:

In [30]:
xmod = x%2
xmod

Out[30]:
array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0])

Then, we select even numbers by asking the modulo to be equal to zero. Again, the == operator is applied to a numpy array, so it is an element-wise operation:

In [31]:
mask = (xmod == 0)

Out[31]:
array([False,  True, False,  True, False,  True, False,  True, False,
True])

With this mask, we select even numbers and return a new array:

In [32]:
x[mask]

Out[32]:
array([ 2,  4,  6,  8, 10])

In fact, the new array is a view on the original array. The data is not copied.

It is possible to invert the mask:

In [33]:
x[~mask]

Out[33]:
array([1, 3, 5, 7, 9])

And of course it's possible to build masks on the fly, which is what is typically done:

In [34]:
x[x%2==0]

Out[34]:
array([ 2,  4,  6,  8, 10])

In python data science, boolean indexing is used extensively to select data by applying thresholds on chosen variables.

Slicing¶

In basic python, a slice is defined as a tuple, start,stop,step. It allows to select a sequence of elements in a sequence (which, by essence in python, is 1D):

In [35]:
lst = list(range(1, 10))
print(lst)
lst[1::2]

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Out[35]:
[2, 4, 6, 8]

We selected elements:

• starting at index 1 (value 2);
• stopping after the end of the list, as stop is not specified. At this stage, the last element, 9, is included;
• in steps of 2. So 9 does not appear.

Exercise:

Play with the slice definition in the cell above. Try to:

• select all odd numbers with a slice
• select all numbers larger or equal to 5
• select all even numbers between 2 and 6

In the example above, we decided not to specify stop. This is possible for all fields:

In [36]:
print( lst[:5:] )
print( lst[::2] )
print( lst[3::] )
print( lst[::] )

[1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
[4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

The notation for these can and should be simplified to:

In [37]:
print( lst[:5] )
print( lst[::2] )
print( lst[3:] )
print( lst[:] )

[1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
[4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Exercise:

Could simplify these expressions further by removing more colons? What would happen if you do? Test your hypotheses in the above cell.

Numpy slicing is a simple generalization of python slicing to multiple dimensions. To test it, we create a 2D matrix with 4 lines and 5 columns. For this, we use the reshape method that will be discussed in the next section:

In [38]:
x = np.arange(20).reshape(4,5)
x

Out[38]:
array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])

We can now use the slice notation on any field of the multidimensional index. Here are a few example:

• select the second column:
In [39]:
x[:, 1]

Out[39]:
array([ 1,  6, 11, 16])
• select the first two columns:
In [40]:
x[:, :2]

Out[40]:
array([[ 0,  1],
[ 5,  6],
[10, 11],
[15, 16]])
• select columns in steps of 2:
In [41]:
x[:, ::2]

Out[41]:
array([[ 0,  2,  4],
[ 5,  7,  9],
[10, 12, 14],
[15, 17, 19]])
• reverse column order, by specifying a -1 step on the last dimension:
In [42]:
x[:, ::-1]

Out[42]:
array([[ 4,  3,  2,  1,  0],
[ 9,  8,  7,  6,  5],
[14, 13, 12, 11, 10],
[19, 18, 17, 16, 15]])

In the previous examples, we have been working on the last dimension, while preserving the first dimension. For this, we needed to add a : as first indexing item to request a loop on the first dimension.

But this can become painful when there are more than 2 dimensions. For example, consider this 3D array:

In [43]:
y = np.arange(8).reshape((2,2,2))
y

Out[43]:
array([[[0, 1],
[2, 3]],

[[4, 5],
[6, 7]]])

It's a 2x2x2 cube. If you want to select the first elements along the last dimension, you can either do:

In [44]:
y[:,:,0]

Out[44]:
array([[0, 2],
[4, 6]])

Or use the ellipsis notation:

In [45]:
y[...,0]

Out[45]:
array([[0, 2],
[4, 6]])

The ellipsis adds as many : as missing dimensions in the indexing statement. In this way, you don't need to keep track of the number of dimensions, and you type less. But most importantly, your code becomes independent of the number of dimensions in the array. For example in this case, y[...,0] selects the first elements along the last dimension whatever the dimension of y:

In [46]:
y = np.arange(8).reshape(2,4)
print(y)
print(y[...,0])

[[0 1 2 3]
[4 5 6 7]]
[0 4]

Numpy array reshaping¶

Reshaping an array consists in re-organizing the array data into a new array with a different shape.

First, reshaping makes it possible to easily create an array with a given shape, as we have done before:

In [47]:
x = np.arange(20).reshape(4,5)
x

Out[47]:
array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])

We created a flat 1D array with integers ranging from 0 to 19, and immediately reshaped it to a 2D array with shape (4,5).

Making an array one-dimensional¶

Often, one needs to flatten an array. This can be done in several ways:

• flatten:
In [48]:
y = x.flatten()
y

Out[48]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
• ravel:
In [49]:
x.ravel()

Out[49]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
• reshape:
In [50]:
x.reshape(-1)

Out[50]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])

The output is the same, so what's the difference between flatten, ravel, and reshape?

First, flatten always returns a copy of the array.

On the other hand, ravel attempts to return a view on the original array, and only copies the data when it's needed. This means that if you modify the output of ravel, you might modify the original array. Also, it means that you should use ravel on very big arrays to save memory, and that ravel will generally be faster than flatten.

reshape is a more flexible function as you can reshape to anything. Just like ravel, it returns a view and only performs a copy when necessary. I suggest to use either flatten or ravel when you want to flatten an array, to make your code more explicit.

Turn a 1D array into a "column" 1D array¶

This means that we want to turn an array of shape (N,) into an array of shape (N,1). Let's consider an array of shape (5,) :

In [51]:
y = np.arange(5)
print(y)
print(y.shape)

[0 1 2 3 4]
(5,)

We turn it into a columnar array of shape (20,1) with the c_ attribute (note that it's not a function!)

In [52]:
coly = np.c_[y]
print(coly)
print(coly.shape)

[[0]
[1]
[2]
[3]
[4]]
(5, 1)

Practical numpy examples in machine learning¶

You should know enough about numpy to deal with most numpy operations encountered in machine learning projects.

We will now work on real-life examples so that you can hone your numpy skills!

Label management¶

Let's consider a practical supervised machine learning example, the classification of dog and cat pictures. The machine is presented with examples so that it can learn. Each example consists in:

• example data, here an image of a dog or a cat
• a target or label, e.g. 0 for dog and 1 for cat

When presented a cat image, the machine will output a value between 0 and 1. We'll compare this value to the target, and update the machine parameters so that next time, it can get closer to the solution.

The training dataset (the set of examples used for training) is typically stored in numpy arrays. For example, one could use two numpy arrays:

• An array of shape (10000, 200, 200, 3) for the images, assuming we have 10000 color pictures of 200x200 pixels.
• An array of shape (10000,) or of shape (10000, 1) for the targets.

The exact structure of the dataset will depend on where you get it or how you build it, and sometimes you will have to change this structure to provide the dataset to the machine, depending on how the machine is implemented.

As a practical case, let's use the simplified MNIST handwritten digit dataset included in the scikit-learn machine learning package. We first load the dataset:

In [53]:
from sklearn import datasets

We get some object called digits. We can use the type and dir built-in functions to see what it is:

In [54]:
print( type(digits) )
print( dir(digits) )

<class 'sklearn.utils.Bunch'>
['DESCR', 'data', 'images', 'target', 'target_names']

Ok, Bunch is some unknown class in scikit-learn, but its attributes have rather explicit names. Let's print some more information:

In [55]:
print( type(digits.images) )
print( type(digits.target) )

<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

Two numpy arrays! You should start to feel at home. Let's print their shape:

In [56]:
print( digits.images.shape )
print( digits.target.shape )

(1797, 8, 8)
(1797,)

Exercises

1. How many examples do you have? What is the size of the images in pixels? Are they color or black and white images?
2. Print the first image and the first label
In [57]:
# your code here

1. Scikit-learn machines expect a 1D array of labels, such as the one we have here. But Keras+Tensorflow expects a columnar array of labels. Convert the array of labels into an array suitable for Keras+Tensorflow:
In [58]:
# your code here

Accuracy calculation¶

Accuracy is a measure of performance for classification algorithms. It is defined the probability to misclassify an example and calculated as

$$a = 1 - \frac{M}{N},$$

where $N$ is the total number of examples, and $M$ the number of misclassified examples.

After training, the accuracy is computed on a test dataset separate from the training dataset. In practice, the whole test dataset is sent as a numpy array to the machine, which produces a numpy array containing the results of the evaluation for each example in the test dataset. The accuracy is then computed from this output and the labels for the test dataset.

Let us assume we're still working on the MNIST handwritten digits dataset and that the test dataset contains 10 examples, with the following labels:

In [59]:
true_labels = np.random.randint(0, 10, 10)
true_labels

Out[59]:
array([8, 7, 5, 8, 9, 9, 8, 6, 4, 6])

And let's assume that the output of our machine for these 10 examples is:

In [60]:
results = np.array([6, 7, 3, 6, 0, 7, 3, 1, 6, 5])
results

Out[60]:
array([6, 7, 3, 6, 0, 7, 3, 1, 6, 5])

Exercise:

Compute the accuracy in per cent. For this, note that you can sum up all entries in a numpy array x by doing x.sum(). No loop allowed!

In [61]:
# your code here

Rescaling image color levels¶

Neural networks don't work well with big numbers.

So usually, the neural network inputs are rescaled to be of the order of unity before being fed to the neural network.

Let's consider again the MNIST dataset, that is already loaded in this notebook as digits, and here is the first image:

In [62]:
digits.images[0]

Out[62]:
array([[ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.],
[ 0.,  0., 13., 15., 10., 15.,  5.,  0.],
[ 0.,  3., 15.,  2.,  0., 11.,  8.,  0.],
[ 0.,  4., 12.,  0.,  0.,  8.,  8.,  0.],
[ 0.,  5.,  8.,  0.,  0.,  9.,  8.,  0.],
[ 0.,  4., 11.,  0.,  1., 12.,  7.,  0.],
[ 0.,  2., 14.,  5., 10., 12.,  0.,  0.],
[ 0.,  0.,  6., 13., 10.,  0.,  0.,  0.]])

Exercise

1. what is the largest greyscale level that can be found in digits.image?
2. rescale the dataset so that the largest number is 1.

No loop allowed!

In [63]:
# your code here

Standardizing neural network inputs¶

We already know that neural networks don't like to deal with large numbers. And it's also not good to have widely different shapes for the distributions of the input variables.

So usually, we standardize the input variables. A common choice is to set the mean of each input distribution to 0, and its standard deviation to 1.

Let's build a toy dataset with 1000 entries and two input variables following a gaussian probability density function.

First, we generate the dataset.

For this, we draw a random sample of 1000 events for each variable. Note the large difference in mean and sigma:

In [64]:
x = np.random.normal(100, 5, 1000)
y = np.random.normal(10, 1, 1000)

Then, we stack the two variables in a single numpy array:

In [65]:
dataset = np.c_[x, y]
print(dataset.shape)
dataset

(1000, 2)

Out[65]:
array([[104.48387071,  10.60128991],
[101.0351927 ,   9.25755183],
[ 98.41009372,   9.80984867],
...,
[100.55546737,  12.65491897],
[ 99.82967835,   9.78244294],
[ 96.13135123,   9.29579931]])

And finally, we check that the mean and standard deviation of each variable is as expected:

In [66]:
print(np.mean(dataset, axis=0))
print(np.std(dataset, axis=0))

[99.85250423 10.00845165]
[5.02189333 0.99061707]

By using axis=0, we asked for the mean and standard deviation to be computed along the first axis, which is the outermost dimension. Therefore, numpy computed these quantities for both columns separately.

Take enough time to think about this, and to understand how you move in the 2D array when you stride the outermost dimension.

Exercise

1. Rescale the dataset so that the mean and standard deviation of both variables are set to 0 and 1, respectively. But:

• no loop allowed of course!
• you're also not allowed to treat the two columns separately to stack them after the fact as we have done above
• only one (short) line of code
In [67]:
# your code here

1. there is a way to recreate the dataset in one line of code. Check the documentation of numpy.random.normal and do it:
In [68]:
# your code here

Changing color scheme from RGB to BGR¶

By convention, TensorFlow considers that the color channels in an image are ordered as red, green, blue (RGB) along the innermost dimension. On the contrary, Caffe, another deep learning library, assumes colors to be in the reverse order (BGR).

Let's assume you want to do transfer learning, and that the deep neural network you want to use has been pre-trained with Caffe. But your images are RGB... Obviously, you need to convert your images from RGB to BGR so that the network can make use of its knowledge of colors.

In this exercise, you will find out how to do this on a single image.

First, let's load the "face" example image:

In [69]:
import scipy.misc
import matplotlib.pyplot as plt
face = scipy.misc.face()
plt.imshow(face)

Out[69]:
<matplotlib.image.AxesImage at 0x7f3711d62f50>

Exercise

• find out about the number of pixels and colors in this image
• make sure that red is the first channel. To do this:
• make a copy of face
• in the copy, set the last two color channels to zero
• display the copy
• convert the image to BGR

Hint: you will have to make good use of the slice notation. Also, try to use the ellipsis.

Conclusion

Well done! you're now an experienced numpy user!

Your new skills will prove useful, even out of the field of machine learning.

Just one word of caution for the future: Don't reinvent the wheel. It's highly probable that what you'll need has already been implemented by others using numpy. So don't hesitate to have a look at existing scientific python packages before starting to code. For example:

• scikit-image provides image manipulation tools
• opencv is a full-fledged computer vision library, and comes with many tools as well.
• scikit-learn has methods for typical array manipulation in machine learning,
• keras provides preprocessing tools if you use Keras+TensorFlow for deep learning
• PyTorch also has many preprocessing functions, in the torchvision, torchaudio, and torchtext.

You can now head over to Matplotlib for Machine Learning to learn how to make your first plots!

And if you liked this article, you can subscribe to my mailing list to be notified of new posts (no more than one mail per week I promise.)

Learn about Data Science and Machine Learning!

You can join my mailing list for new posts and exclusive content:


text:
Inverse of a matrix can find out in many ways. Commands Used LinearAlgebra[MatrixInverse] See Also LinearAlgebra , Matrix Palette ... is the leading provider of high-performance software tools for engineering, science, and mathematics. Because it only uses comparisons to operate on elements, it is a comparison sort. 7. All we had to do was swap 2 elements and put negative signs in front of 2 elements and then divide each element by the determinant. time to compute matrix inverse. In these situations the matrices must be what’s called conformable. This page has a C Program to find the Inverse of matrix for any size of matrices. Finding the inverse matrix of a 2x2 matrix is relatively easy. You should have created a matrix of 5*10 or 10*20 or … A 3 x 3 matrix has 3 rows and 3 columns. If the determinant is 0, the matrix has no inverse. Signed decimal integers. Matrix inverse. Calculate adjoint of matrix. Here you will get java program to find inverse of a matrix of order 2×2 and 3×3. To find the inverse of a matrix, firstly we should know what a matrix is. Thus the system of linear equations is badly conditioned, but consistent. Time Complexity of this algorithm is O(n 2). I find the modular multiplicative inverse (of the matrix determinant, which is $1×4-3×5=-11$) with the extended Euclid algorithm (it is $-7 \equiv 19 \pmod{26}$). Here you will get C and C++ program to find inverse of a matrix. Hexadecimal output in the form. Big list of c program examples It is seldom necessary to form the explicit inverse You clicked a link that corresponds to this MATLAB command: Run the command by entering it in the MATLAB Command Window. For large and small Hessian, the iterations will behave like gradient descent with step size /. C++ Program to Find Inverse of a Graph Matrix Display message by format with the printf The number of characters actually printed. to avoid this condition. The goal is to make Matrix A have 1s on the diagonal and 0s elsewhere (an Identity Matrix) ... and the right hand side comes along for the ride, with every operation being done on it as well.But we can only do these \"Elementary Row Ope… If a determinant of the main matrix is zero, inverse doesn't exist. * You can use all the programs on www.c-program-example.com This is a C++ program to Find Inverse of a Graph Matrix. It is imported and implemented by … Signed decimal integers. Read more about C Programming Language . The inverse of a matrix exists only if the matrix is C program to find determinant of a matrix 12. Below are implementation for finding adjoint and inverse of a matrix. Now I want to use the program which uses LAPACK to find inverse of a large matrix but I do not know how to compile the code using these libraries. When u are going to deal with n*2n matrix, why create a matrix of size 10*10. Set the matrix (must be square) and append the identity matrix of the same dimension to it. Below I have shared program to find inverse of 2×2 and 3×3 matrix. home Front End HTML CSS JavaScript HTML5 Schema.org php.js Twitter Bootstrap Responsive Web Design tutorial Zurb Foundation 3 tutorials Pure CSS HTML5 Canvas JavaScript Course Icon Angular React Vue Jest Mocha NPM Yarn Back End PHP Python Java Node.js … Have another way to solve this solution? Inverse of an identity [I] matrix is an identity matrix [I]. Perform the Inverse Probability Cumulative Density Analysis on t-Distribution in R Programming - qt() Function; Inverse of Matrix in R; Check if the Object is a Matrix in R Programming - is.matrix() Function; Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix… Its symbol is the … Inverse Matrix 3 ; Radix Sort 6 ; Inverse of a non square matrix 2 ; help matrix 1 ; Visual C++ program compiles, but won't run 5 ; matrix operation 8 ; matrix code 2 ; Retrieve particular row and display it in database 2 ; matrix multiplication help.. 5 ; Sparse Matrix 6 ; Need advice on parallelising an algorithm using OpenMP 0 Next » This C program sorts a given array of integer numbers using Bubble Sort technique. See mldivide for Documentation All; Examples; Functions; Videos; Answers; Main Content. w3resource . This produces the solution using Gaussian elimination, without explicitly We just mentioned the "Identity Matrix". the inverse of New York: Schaum Publishing Co., 1962. I have the matrix$$\begin{pmatrix} 1 & 5\\ 3 & 4 \end{pmatrix} \pmod{26}$$ and I need to find its inverse. where In is Calculate adjoint of matrix. This is the last function in LinearAlgebraPurePython.py in the repo. If the determinant of 4x4 matrix is zero, … is badly scaled or nearly singular. Doing so gives us matrix([[ 0.3, -0.2],[-0.7, 0.8]]) as the inverse matrix. The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns. if A is a Square matrix and |A|!=0, then AA’=I (I Means Identity Matrix).Read more about C Programming Language . All we had to do was swap 2 elements and put negative signs in front of 2 elements and then divide each element by the determinant. Data Types: single | double It is clear that, C program has been written by me to find the Inverse of matrix for any size of square matrix.The Inverse of matrix is calculated by using few steps. Calculate the inverse of the matrix. Examine why solving a linear system by inverting the matrix using inv(A)*b is inferior to solving it directly using the backslash operator, x = A\b. Enter a matrix. Next, transpose the matrix by rewriting the first row as the first column, the middle row as the middle column, and the third row as the third column. In general, the inverse of n X n matrix A can be found using this simple formula: where, Adj(A) denotes the adjoint of a matrix and, Det(A) is Determinant of matrix A. There are definitely flaws in the way I'm approaching this problem, but for the life of me I cannot figure how to write these methods. It is input by the user. C Program to Find Transpose of a Matrix In this example, you will learn to find the transpose of a matrix in C programming. For sparse inputs, inv(X) creates We can find inverse of a matrix in following way. Next: Write a NumPy program to calculate the QR decomposition of a given matrix. if there exists a matrix Y of the same size such Commands Used LinearAlgebra[MatrixInverse] See Also LinearAlgebra , Matrix Palette . A matrix X is invertible if there exists a matrix Y of the same size such that X Y = Y X = I n, where I n is the n-by-n identity matrix. Java program to Inverse Of A Matrixwe are provide a Java program tutorial with example.Implement Inverse Of A Matrix program in Java.Download Inverse Of A Matrix desktop application project in Java with source code .Inverse Of A Matrix program for student, beginner and beginners and professionals.This program help improve student basic fandament and logics.Learning a basic … I have to create methods to find the adjoint and inverse of the matrices. inv is unable to check for this condition. Use rcond or cond to check the condition number of The values in the array are known as the elements of the matrix. Figure 2 Matrix Multiplication. Using determinant and adjoint, we can easily find the inverse of a square matrix using below formula, If det(A) != 0 A-1 = adj(A)/det(A) Else "Inverse doesn't exist" Inverse is used to find the solution to a system of linear equation. If the inverse of matrix A, A-1 exists then to determine A-1 using elementary row operations. Inverse of a matrix can find out in many ways. Next » This C program sorts a given array of integer numbers using Bubble Sort technique. It is input by the user. Let us consider three matrices X, A and B such that X = AB. I do love Jupyter notebooks, but I want to use this in scripts now too. C++ Program to Calculate the Inverse of matrix. This is a C++ program to Find Inverse of a Graph Matrix. To find the Matrix Inverse, matrix should be a square matrix and Matrix Determinant is should not Equal to Zero. I'm following the adjoint method (first calculation of the adjoint matrix, then transpose this matrix and finally, multiply it for the inverse of the value of the determinant). The matrix Y is called the inverse of X. C Program to find the Inverse of a Matrix. solving the system of linear equations Ax = b. Find the determinant of each of the 2x2 minor matrices, then create a matrix of cofactors using the results of … If the determinant of the matrix is zero, then the inverse does not exist and the matrix is singular. C++ Program to Calculate the Inverse of matrix. In this program, the user is asked to enter the number of rows r and columns c. Their values should be less than 10 in this program. To find the inverse of a 2x2 matrix: swap the positions of a and d, put negatives in front of b and c, and divide everything by the determinant (ad-bc). the n-by-n identity matrix. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity matrix. I do it according to this website. Complex Number Support: Yes. First calculate deteminant of matrix. Then calculate adjoint of given matrix. If the determinant of 4x4 matrix is non zero, then we can find Inverse of matrix. Create a script file and type the following code − 1) Frank Aryes, Jr., Theory and Problems of Matrices. The fact that err_inv and err_bs are both on the order of 1e-6 simply reflects the condition number of the matrix. I'm creating a program to calculate matrix computations. Sometimes there is no inverse at all Multiplying Matrices Determinant of a Matrix Matrix Calculator Algebra Index. Now, solve the same linear system using the backslash operator \. Contact Maplesoft Request Quote. If X is Steps involved in the Example To … The inverse of a matrix exists only if the matrix is non-singular i.e., determinant should not be 0. In this program, the user is asked to enter the … To find the Inverse of a 3 by 3 Matrix is a little critical job but can be evaluated by following few steps. I have the matrix$$\begin{pmatrix} 1 & 5\\ 3 & 4 \end{pmatrix} \pmod{26}$$ and I need to find its inverse. The gpuArray When we multiply a matrix by its inverse we get the Identity Matrix (which is like "1" for matrices): A × A-1 = I. 5. Tags for Inverse Matrix of 3x3 in C. 3*3 matrix inverse program in c; c program for adjoint of matrix; inverse matrix 3x3 c ; inverse of a matrix c program; inverse of a matrix using c program; c; inverse 3x3 matrix c ; inverse matrix 3x3 coding in java; program matriks 3x3 determinan dan invers di c Identity Matrix. Inverse of a Matrix using Elementary Row Operations. badly scaled or nearly singular, then the inv calculation We can find inverse of a matrix in following way. Create a random matrix A of order 500 that is constructed so that its condition number, cond(A), is 1e10, and its norm, norm(A), is 1. Since inv performs the matrix inversion using floating-point computations, in practice Y*X is close to, but not exactly equal to, the identity matrix eye(size(X)). A better way, from the standpoint of both execution time and numerical example. Also called the Gauss-Jordan method. Inverse of a Matrix is important for matrix operations. Here is the source code of the C program to sort and display the integer array. a sparse identity matrix and uses backslash, X\speye(size(X)). Decimal floating point. I'm trying to calculate the inverse matrix in Java. To calculate inverse matrix you need to do the following steps. This means that we can find the solution for the system using the inverse of the matrix provided that B is given. So we multiply each element in the array by 1/10. print(np.allclose(np.dot(ainv, a), np.eye(3))) Notes. I do it according to this website. The MATLAB Finding the inverse of the Hessian in high dimensions to compute the Newton direction = ... is to add a scaled identity matrix to the Hessian, , with the scale adjusted at every iteration as needed. Contribute to md-akhi/Inverse-matrix development by creating an account on GitHub. Here we find out inverse of a graph matrix using adjoint matrix … Time Complexity of this algorithm is O(n. © 2011-2020 Sanfoundry. inv function prints a warning if X A square matrix is singular only when its determinant is exactly zero. All Rights Reserved. Finding an Inverse Matrix by Elementary Transformation. It is "square" (has same number of rows as columns), It has 1s on the diagonal and 0s everywhere else. As a result you will get the inverse calculated on the right. printf("\n Please Enter Number of rows and columns \n"); Below C Programming scanf statement will assign the user entered values to i and j (Rows = i and Columns = j).. scanf("%d %d", &i, &j); The below for loop in this C program will help to iterate each cell present in a[2][3] matrix. … Decimal floating point. Chapter 2: Some Types of Matrices, p. 11, Example 1 ← PREV … Inverse of a matrix in MATLAB is calculated using the inv function. that differ from MATLAB® results. Doing so gives us matrix([[ 0.3, -0.2],[-0.7, 0.8]]) as the inverse matrix. Matrix multiplication is best explained by example. Matrix Calculator have all matrix functions having 'm' rows and 'n' columns. If the number of rows and columns in a matrix is a and b respectively, then the … As a result you will get the inverse calculated on the right. Use the Break Statement in C++ language It's used in switch case control structure after the case blocks. Strassen's matrix multiplication program in c 11. A_M has morphed into an Identity matrix, and I_M has become the inverse of A.Yes! Earlier in Matrix Inverse Using Gauss Jordan Method Algorithm, we discussed about an algorithm for finding inverse of matrix of order n. In this tutorial we are going to develop pseudocode for this method so that it will be easy while implementing using programming language. The exact solution x is a random vector of length 500, and the right side is b = A*x. C++ Program to Find Inverse of a Graph Matrix Display message by format with the printf The number of characters actually printed. Y = inv(X) computes A square matrix is Sanfoundry Global Education & Learning Series – 1000 C Programs. Finally divide adjoint of matrix by determinant. collapse all in page. A-1 × A = I. C Program to Find Inverse of a Matrix « Prev. that XY=YX=In, further information. Scientific notation. Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Inverse of a matrix exists only if the matrix is non-singular i.e., determinant should not be 0. So we multiply each element in the array by 1/10. This results in slower but more reliable convergence where the Hessian doesn't provide useful information. This program finds the inverse of a matrix and prints the result on the compiler screen. – nicola Nov 16 '15 at 16:03 The C program is successfully compiled and run on a Linux system. Previous: Write a NumPy program to compute the determinant of an array. accuracy, is to use the matrix backslash operator x = A\b. Inverse of a Matrix Description Calculate the inverse of a matrix. Other MathWorks country sites are not optimized for visits from your location. MathWorks is the leading developer of mathematical computing software for engineers and scientists. Finding the inverse matrix of a 2x2 matrix is relatively easy. Hexadecimal output in the form. A matrix is a function which includes an ordered or organised rectangular array of numbers. Tags for Inverse Matrix of 3x3 in C. 3*3 matrix inverse program in c; c program for adjoint of matrix; inverse matrix 3x3 c ; inverse of a matrix c program; inverse of a matrix using c program; c; inverse 3x3 matrix c ; inverse matrix 3x3 coding in java; program matriks 3x3 determinan dan invers di c To determine the inverse of a matrix using elementary transformation, we convert the given matrix into an identity matrix. Syntax. And by ALSO doing the changes to an Identity Matrix it magically turns into the Inverse! The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variables.

## program to find inverse of a matrix

Rockjam 61 Key Keyboard Review, Riviana Foods Jobs, Fairways Hoa Limerick, Pa, Uber Eats Australia Complaints, Mapigano Lion Guard, Impact 2020 Schedule,

format: plain


text:
• Purchase/rental options available:
Abstract

We provide a definition for an extended system of $\gamma$-factors for products of generic representations $\tau$ and $\pi$ of split classical groups or general linear groups over a non-archimedean local field of characteristic $p$. We prove that our $\gamma$-factors satisfy a list of axioms (under the assumption $p\neq 2$ when both groups are classical groups) and show their uniqueness (in general). This allows us to define extended local $L$-functions and root numbers. We then obtain automorphic $L$-functions $L(s,\tau \times \pi)$, where $\tau$ and $\pi$ are globally generic cuspidal automorphic representations of split classical groups or general linear groups over a global function field. In addition to rationality and the functional equation, we prove that our automorphic $L$-functions satisfy the Riemann Hypothesis.

• Purchase/rental options available:


text:
# Matrix expansion

• August 7th 2012, 10:33 AM
Greymalkin
Matrix expansion
Not sure how to input matrices into latex but its an easy problem:

A=
|7 -4 -6 |
|2 0 -3 |
|1 2 -5 |

Evaluate |A| by expanding down the third column:
I get:
[-6(-1)4(3)]+[-3(-1)5(18)]+[-5(-1)6(8)]
=-6(3)+3(18)-5(8)
=-4

answer is supposed to be -10, which i've arrived at expanding another row and column, what am I doing wrong?(Clapping)
• August 7th 2012, 10:43 AM
Plato
Re: Matrix expansion
Quote:

Originally Posted by Greymalkin
Not sure how to input matrices into latex but its an easy problem:

A=
|7 -4 -6 |
|2 0 -3 |
|1 2 -5 | Evaluate |A| by expanding down the third column:

$-6(4)+3(18)-5(8)=-10$
• August 7th 2012, 10:48 AM
Greymalkin
Re: Matrix expansion
ahh silly mistake 1x0, thank you very much sir!
• August 7th 2012, 10:51 AM
HallsofIvy
Re: Matrix expansion
Quote:

Originally Posted by Greymalkin
Not sure how to input matrices into latex but its an easy problem:

A=
|7 -4 -6 |
|2 0 -3 |
|1 2 -5 |

Evaluate |A| by expanding down the third column:
I get:
[-6(-1)4(3)]

How did you get "3" here? $6\left|\begin{array}{cc}2 & 0 \\ 1 & 2\end{array}\right|= 6(4)$

Quote:

+[-3(-1)5(18)]+[-5(-1)6(8)]
=-6(3)+3(18)-5(8)
=-4

answer is supposed to be -10, which i've arrived at expanding another row and column, what am I doing wrong?(Clapping)
• August 7th 2012, 11:17 AM
Soroban
Re: Matrix expansion
Hello, Greymalkin!

Quote:

$A\:=\:\begin{vmatrix}7&\text{-}4&\text{-}6 \\2&0&\text{-}3 \\ 1&2&\text{-}5\end{vmatrix}$

$\text{Evaluate }|A|\text{ by expanding down the third column.}$

$\text{I get: }\:[\text{-}6(\text{-}1)^4(3)]+[\text{-}3(\text{-}1)^5(18)]+[\text{-}5(\text{-}1)^6(8)]$ . Not sure what all this is.
. . . $=\;\text{-}6(3)+3(18)-5(8) \;=\;-4$

Answer is supposed to be -10, which i've arrived at expanding another row and column.
What am I doing wrong?

Down the third column . . .

. . $\text{-}6\begin{vmatrix}2&0\\1&2\end{vmatrix} - (\text{-}3)\begin{vmatrix}7&\text{-}4 \\ 1&2 \end{vmatrix} - 5 \begin{vmatrix}7 & \text{-}4 \\ 2 & 0 \end{vmatrix}$

. . $=\;-6\underbrace{(4-0)}_{\text{Here!}} + 3(14 +4) - 5(0+8)$

. . $=\;-24 + 54 - 40$

. . $=\;-10$


text:
Check GMAT Club Decision Tracker for the Latest School Decision Releases https://gmatclub.com/AppTrack
GMAT Club

 It is currently 23 Mar 2017, 03:30

### GMAT Club Daily Prep

#### Thank you for using the timer - this advanced tool can estimate your performance and suggest more practice questions. We have subscribed you to Daily Prep Questions via email.

Customized
for You

we will pick new questions that match your level based on your Timer History

Track

every week, we’ll send you an estimated GMAT score based on your performance

Practice
Pays

we will pick new questions that match your level based on your Timer History

# Events & Promotions

###### Events & Promotions in June
Open Detailed Calendar

# If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0

Author Message
TAGS:

### Hide Tags

Manager
Joined: 08 Oct 2010
Posts: 213
Location: Uzbekistan
Schools: Johnson, Fuqua, Simon, Mendoza
WE 3: 10
Followers: 10

Kudos [?]: 703 [4] , given: 974

If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

09 Dec 2010, 07:39
4
KUDOS
4
This post was
BOOKMARKED
00:00

Difficulty:

75% (hard)

Question Stats:

54% (02:21) correct 46% (01:35) wrong based on 325 sessions

### HideShow timer Statistics

If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 has equal roots then the value of q is

A. 49/4
B. 4/49
C. 4
D. 1/4
E. 12
[Reveal] Spoiler: OA
Math Expert
Joined: 02 Sep 2009
Posts: 37552
Followers: 7391

Kudos [?]: 99221 [3] , given: 11009

### Show Tags

09 Dec 2010, 08:06
3
KUDOS
Expert's post
3
This post was
BOOKMARKED
feruz77 wrote:
If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 has equal roots then the value of q is
a) 49/4
b) 4/49
c) 4
d) 1/4
e) 12

As one of the roots of $$x^2+px+12=0$$ is $$x=4$$ then substituting we'll get: $$16+4p+12=0$$ --> $$p=-7$$.

Now, the second equation becomes $$x^2-7x+q=0$$. As it has equal roots then its discriminant must equal to zero: $$d=49-4q=0$$ --> $$q=\frac{49}{4}$$.

_________________
Manager
Joined: 14 Dec 2010
Posts: 57
Followers: 0

Kudos [?]: 2 [1] , given: 4

### Show Tags

16 Dec 2010, 08:31
1
KUDOS
Hi

I guess the answer is 12.

X^2+px+12=0 with one root as 4

ie 4+b=-p where b is another root
and 4b=12,then b=3

From second equation X^2+px+q=0 ,as roots are equal ,then first root ie 4 and second root ie b=3 are the roots for second equation as well,making 4*3=q ie 12 Ans E
Math Expert
Joined: 02 Sep 2009
Posts: 37552
Followers: 7391

Kudos [?]: 99221 [1] , given: 11009

### Show Tags

16 Dec 2010, 08:42
1
KUDOS
Expert's post
Eshika wrote:
Hi

I guess the answer is 12.

X^2+px+12=0 with one root as 4

ie 4+b=-p where b is another root
and 4b=12,then b=3

From second equation X^2+px+q=0 ,as roots are equal ,then first root ie 4 and second root ie b=3 are the roots for second equation as well,making 4*3=q ie 12 Ans E

It's given that the roots of x^2+px+q=0 are equal (so given that it has double root), not that the roots of this equation equal to the roots of another: x^2+px+12=0. If it were as you say then we would have q=12 right away:
x^2+px+q=0;
x^2+px+12=0.

OA for this question is A.
_________________
Manager
Joined: 14 Dec 2010
Posts: 57
Followers: 0

Kudos [?]: 2 [0], given: 4

### Show Tags

16 Dec 2010, 22:22
Bunuel wrote:
Eshika wrote:
Hi

I guess the answer is 12.

X^2+px+12=0 with one root as 4

ie 4+b=-p where b is another root
and 4b=12,then b=3

From second equation X^2+px+q=0 ,as roots are equal ,then first root ie 4 and second root ie b=3 are the roots for second equation as well,making 4*3=q ie 12 Ans E

It's given that the roots of x^2+px+q=0 are equal (so given that it has double root), not that the roots of this equation equal to the roots of another: x^2+px+12=0. If it were as you say then we would have q=12 right away:
x^2+px+q=0;
x^2+px+12=0.

OA for this question is A.

Yeah I gt that..My mistake...
Manager
Joined: 07 Feb 2010
Posts: 159
Followers: 2

Kudos [?]: 589 [2] , given: 101

### Show Tags

18 Dec 2010, 08:25
2
KUDOS
Eshika wrote:
Hi

I guess the answer is 12.

X^2+px+12=0 with one root as 4

ie 4+b=-p where b is another root
and 4b=12,then b=3

From second equation X^2+px+q=0 ,as roots are equal ,then first root ie 4 and second root ie b=3 are the roots for second equation as well,making 4*3=q ie 12 Ans E

so the 2 roots of equation are 4 & 3
p = - (4+3) =-7
so 2 eq becomes x^2-7x+q
it is given 2 roots of the equation are same
a=b
a+b =7, 2a=7
a=7/2
q = a^2 = 49/4
Manager
Joined: 14 Dec 2010
Posts: 57
Followers: 0

Kudos [?]: 2 [0], given: 4

### Show Tags

19 Dec 2010, 22:07
feruz77 wrote:
If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 has equal roots then the value of q is
a) 49/4
b) 4/49
c) 4
d) 1/4
e) 12

Thanks feruz77,I just started using GMAT club and got to know the concept of kudos after getting a mail that I have received 1 kudos.Will start using them now onwards.
GMAT Club Legend
Joined: 09 Sep 2013
Posts: 14367
Followers: 601

Kudos [?]: 174 [0], given: 0

Re: If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

27 Sep 2013, 08:59
Hello from the GMAT Club BumpBot!

Thanks to another GMAT Club member, I have just discovered this valuable topic, yet it had no discussion for over a year. I am now bumping it up - doing my job. I think you may find it valuable (esp those replies with Kudos).

Want to see all other topics I dig out? Follow me (click follow button on profile). You will receive a summary of all topics I bump in your profile area as well as via email.
_________________
Intern
Joined: 03 Jul 2015
Posts: 40
Followers: 0

Kudos [?]: 15 [0], given: 27

If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

12 Sep 2015, 03:13
Bunuel wrote:
feruz77 wrote:
If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 has equal roots then the value of q is
a) 49/4
b) 4/49
c) 4
d) 1/4
e) 12

As one of the roots of $$x^2+px+12=0$$ is $$x=4$$ then substituting we'll get: $$16+4p+12=0$$ --> $$p=-7$$.

Now, the second equation becomes $$x^2-7x+q=0$$. As it has equal roots then its discriminant must equal to zero: $$d=49-4q=0$$ --> $$q=\frac{49}{4}$$.

can you please explain this As it has equal roots then its discriminant must equal to zero i can not understand
Math Forum Moderator
Joined: 20 Mar 2014
Posts: 2651
Concentration: Finance, Strategy
Schools: Kellogg '18 (M)
GMAT 1: 750 Q49 V44
GPA: 3.7
WE: Engineering (Aerospace and Defense)
Followers: 122

Kudos [?]: 1401 [1] , given: 789

If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

12 Sep 2015, 06:21
1
KUDOS
Expert's post
2
This post was
BOOKMARKED
anik19890 wrote:
Bunuel wrote:
feruz77 wrote:
If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 has equal roots then the value of q is
a) 49/4
b) 4/49
c) 4
d) 1/4
e) 12

As one of the roots of $$x^2+px+12=0$$ is $$x=4$$ then substituting we'll get: $$16+4p+12=0$$ --> $$p=-7$$.

Now, the second equation becomes $$x^2-7x+q=0$$. As it has equal roots then its discriminant must equal to zero: $$d=49-4q=0$$ --> $$q=\frac{49}{4}$$.

can you please explain this As it has equal roots then its discriminant must equal to zero i can not understand

Do not post multiple posts for the same doubt. Give the experts/posters some time to reply to your earlier post.

As for your question, refer to the text below:

For a quadratic equation $$ax^2+bx+c=0$$, with a$$\neq$$0

Discriminant, $$D = b^2-4ac$$

For a quadratic equation to have 2 distinct real roots: $$D > 0$$

For a quadratic equation to have 2 equal real roots: $$D = 0$$

For a quadratic equation to have 0 distinct real roots: $$D < 0$$

Coming back to the quadratic equation, $$x^2-7x+q=0$$ ---> for equal roots, $$D =0$$ ---> $$(-7)^2-4*q*1 = 0$$ ---> $$49=4q$$---> $$q= 49/4 = 12.25$$

Hope this helps.
_________________

Thursday with Ron updated list as of July 1st, 2015: http://gmatclub.com/forum/consolidated-thursday-with-ron-list-for-all-the-sections-201006.html#p1544515
Inequalities tips: http://gmatclub.com/forum/inequalities-tips-and-hints-175001.html
Debrief, 650 to 750: http://gmatclub.com/forum/650-to-750-a-10-month-journey-to-the-score-203190.html

Senior Manager
Status: DONE :)
Joined: 05 Sep 2016
Posts: 411
Location: United States (WI)
Concentration: Marketing, Technology
WE: Other (Law)
Followers: 3

Kudos [?]: 14 [0], given: 283

Re: If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

10 Oct 2016, 15:54

--> 7 +- [sqrt(49-(4)(1)(c))]/2

Try values in for C. The correct one should give you the same two values once the equation is solved
Manager
Joined: 16 Mar 2016
Posts: 137
Location: France
GMAT 1: 660 Q47 V33
GPA: 3.25
Followers: 3

Kudos [?]: 31 [0], given: 0

Re: If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

22 Oct 2016, 09:12
x²+px+12=0

if 4 is a root, then

(x - 4) ( x +r2) = 0 ; R2 being the other root.
So, -4*r2 = 12 => r2 = -7

So, x²+px+q= x²-7x+q=0

We know that this equation has only one root, thus :
(x+r)²=0=x²+2xr+r²=0

So, q=r² and 2r=-7
r=-7/2, and q=r²=49/4

Manager
Joined: 11 Jul 2016
Posts: 86
Followers: 0

Kudos [?]: 11 [0], given: 87

Re: If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0 [#permalink]

### Show Tags

22 Oct 2016, 12:17
x^2+px+12=0 is 4

One root is given as 4 => Substituting the values

4p +28 = 0

p = -7

Second eqn x^2+px+q=0

x^2 -7x + q = 0

q = 49/4 A
Re: If one root of x^2+px+12=0 is 4, and the equation x^2+px+q=0   [#permalink] 22 Oct 2016, 12:17
Similar topics Replies Last post
Similar
Topics:
19 Which of the following equations has 1 + √2 as one of its roots? 9 15 Jun 2016, 01:40
9 If there is exactly one root of the equation x^2 + ax + b 9 03 Mar 2012, 18:14
1 If there is exactly one root of the equation x^2 + ax + b 8 23 Aug 2011, 23:17
4 If one root of the equation 2x^2 + 3x – k = 0 is 6, what is the value 6 17 Apr 2011, 09:17
2 5 roots in one equation 4 14 Jan 2010, 05:31
Display posts from previous: Sort by


text:
Select Page

Product of Factors

Perfect square as a product of two factors

In case of perfect square number we have odd number of factors i.e. the number of factors are odd hence in that case required number of ways in which we can write perfect square number as a product of its two factors are (n – 1)/2 if we do not include the square root of the number and (n + 1 )/2 if we include the square root of the number. So number of ways to express a perfect square as product of two different factors (that means excluding its square root) is
½ {(p + 1)(q + I)(r + I) … – 1)}. And if we include the square root then required numbs 1/2 {(p+1)(q+1)(r+1) … +1}

Let’s take one example to understand this.
Example 2: In how many ways you can express 36 as the product of two of its factors?
Solution : Step 1: Prime factorization of 36 i.e. we write 36 = 22 x 32
Step 2: Number of factors of 36 will be (2+1)(2+1)=9 (i.e. factors are 1,2,3,4,6,9,12,18,36)
Step 3: Since we are asked total number of ways hence we include square root of 36 i.e. 6 as well. Thus number of ways you can express 36 as the product of two of its factors is (9+1)/2=5

Product of two co-prime numbers

To express the number as a product of co-prime factors we will use the following steps:

Step 1:Write Prime factorisation of given number i.e. convert the number in the form where p1, p2,p3…..pn are prime numbers and a,b,c….. are natural numbers as their respective powers.
Step 2: In the above step we have n prime numbers then the number of ways to express the number as the product of two co prime numbers =2n-1
Because two primes are always co-prime and after we pick 1 prime the other prime can be picked in 2n-1ways.Hence number of ways in which we can write given number as a product of two co prime factors =2n-1

Example 1: In how many ways you can write 315 as product of two of its co-prime factors.
Solution:

Step 1: Prime factorization of 315 i.e. we write 315  = 32 x 51 x 71
Step2: In above step 3 prime numbers are used. Hence number of ways are 23-1 = 4.Infact we can mention these cases as well 9×35, 5 x 63, 7 x 45, 15 x 21.

Remember: The number of numbers, which are less than N= pa qb rc(where p, q, r are prime numbers and the a,b,c are natural numbers as
their respective powers)and are co-prime to N is given by   $N\left\{ \left( 1-\frac{1}{p} \right)\left( 1-\frac{1}{q} \right)\left( 1-\frac{1}{r} \right) \right\}$

Product of all the factors

To find product of all the factors we follow the following steps:

Step1: Prime factorization N= apbqcr
Step2: Number of factors   (say X)
Step3: Product of all the factors is given by =  NX/2

Example 2: Find the product of all the factors of 120 ?
Solution:
Step 1: Prime factorization : = 23X51X31
Step2: Number of factors are ( 3+1)(1+1)(1+1)= 16
Step 3: Product of all the factors =( 23X51X31)16/2 =(120)8

Number of Co-Prime Numbers that are less than Given Number
When number n is written in the form of n = apbqcr ….. where a, b, c …. are the numbers. The number of co-primes to number n and also less than n is given by . This is also denoted by $n\,\left( 1-\frac{1}{a} \right)\,\left( 1-\frac{1}{b} \right)\,\left( 1-\frac{1}{c} \right)$
N(o) Euler’s totient.
And sum of all these co-primes are given by N × N(Ø)=$\frac{{{n}^{2}}}{2}\left( 1-\frac{1}{a} \right)\,\left( 1-\frac{1}{b} \right)\,\left( 1-\frac{1}{c} \right)$ ……..

Example 1: Find the number of co-primes to 90 that are less than 90.
Solution:
Since 90 = 21 × 32 × 51 so number of numbers that are less than 90 and co-prime to 90
is 90(Ø) $=90\,\left( 1-\frac{1}{2} \right)\,\left( 1-\frac{1}{3} \right)\,\left( 1-\frac{1}{5} \right)=24$

Example 2: Find the sum of all the co-prime numbers to 90 and less than 90.

Solution:
Sum of all the co-prime numbers less than 90 is 90 × 90(Ø) = 90 × 24 = 2160

Product of all the Factors

Write the number in prime factorization format N = apbqcr …..
We know that number of factors of N is given by n(F)= (p + 1)(q + 1)(r + 1).
Then product of all the factors of given by
Let us understand this by taking one example.

Example 3: Find the product of all the factors of 60.
Solution:
1st we will list down all the factors of 60 these are 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60.
Now we will list down all these factors in the form of prime factorization form
1 = 203050
2 = 213050
3 = 203150
and so on then product of all these factors is
(203050)(21)(31)(22)(51)(2131)(2151)(2231)(3151)(2251)(213151)(223151)

= (21+2+1+1+2+2+1+2)(31+1+1+1+1+1)(51+1+1+1+1+1)
= (223151)6 = 606

Example 4: find the product of all the factors of 100.

Solution:
Product of all the factors is calculated as we have done in previous example –
(1)(21)(22)(51)(2151)(2251)(52)(2152)(2252) = (21+2+1+2+1+2)(51+1+1+2+2+2) = (2252)9/2 = 2959

Pair of Factors that are Co-Prime to each other

Write the given number in prime factorization format n = apbq (If number has two prime factors) then number of pairs of factors which are co-prime to each other
= (p +1)(q+ 1) + pq =2pq +(p +q) +1

Let us understand the logic behind this with an example:
Example 5: How many pairs of factors of number 108 such that they are co-prime to each other?

Solution:

We know that 108 = 2233 let two factors are a and b, it is given that they are co-prime to each other.
Case (i) when A = 1, then B can take any factor of 108 so total (2+1)(3+1) such pairs exist.
Case (ii) when A = 21 then we have B = 31 or 32 or 33 = 3 such numbers
Case (iii) when A = 22 then we have B = 31 or 32 or 33 = 3 such numbers
Hence total such numbers is (2+1)(3+1) +2 ×3 = 12 + 6 = 18
Now let us generalize if number is in the form of N = apbq

Let the above number has two factors as (A, B) and they are Co-prime to each other now to find such pairs let us see different cases: –
Case (i): When A = 1 then B can be any factor of number N hence total such values are (p + 1)(q + 1)
Case (ii): When A = ax then B can take any value from 31, or 32 or ….. 3q for one value of ‘x’ there exist ‘q’ such values of B and number of possible values of ‘x’ is ‘p’ hence total such pairs are ‘pq
Hence total number of such pairs are (p + 1)(q + 1)+ pq = 1+ (p + q) + 2pq

Example 6: How many pairs of factors of number 196 such that they are co-prime to each other?
Solution:
We know that 196 = 22 × 72 in this case we have only 2 prime factors hence number of such pairs is given by 1 +4 + 2 × 4 = 13 pairs
If three prime factors then Number n = apbqcr
Let the above number has two factors as (A, B) and they are co-prime to each other now to find such pairs let us see different cases:

Case (i): When A = 1 then B can assume any value from all the factors of number n and total such values of B is (p + 1)(q+ 1)(r +1) = pqr + pq + pr + qr + 1

Case (ii): When A is in the form ax (Total p such value of x exist) then B can assume any factor of number bqcr except 0, and total number of factors of bqcr except 1 is qr hence total such pairs  is ‘pqr’.
Similarly when A is in the form of by and cz then also we will get ‘pqr’ such pairs. Hence total such pairs in this format is 3pqr.

Case (iii): When a is in the form of ax (Total q such values of x exist) and B is in the form of by (y can assume q values) total such pairs are pq, similarly for the combination of ax & cz and by & cz we will get pr and qr such pairs respectively. So total number of such pairs is pq + pr + qr
Hence total number of pairs is 1+ (p+q + r)+ 2(pq +pr + qr) + 4pqr

Example 7: How many pairs of factors of number 300 such that they are co-prime to each other?
Solution:
Since  300 = 22 × 31 × 52 here we have three prime factors hence number of such pairs is given by the formula 1 + (p +q +r)+2(pq +pr +qr)+4pqr here p = 2, q = 1 and r = 2
Hence number of such pairs is 1 +5 + 2 (2 + 2 + 4) = 22 pairs.

Example 8: How many pairs of factors of number 2700 such that they are co-prime to each other?
Solution:
Since 2700 = 22 × 33 × 52 here we have three prime factors hence number of such pairs is given by the formula 1 + (p +q +r) +2(pq +pr +qr) +4pqr here p = 2, q = 3 and r = 2
Hence number of such pairs is 1 + 7+ 2 (6 + 6 + 4) = 1+ 7 + 32 = 40 pairs.
Pairs of Factors that are co-prime to Each Other
In all cases let n has total number of factors as F then
N = apbq then number of such pairs = 1 + (p + q) + 2pq
N = apbqcr then number of such pairs 1 + (p + q + r) +2(pq +pr +qr) + 4pqr
If N = apbqcrds then number of such pairs 1 + (p +q +r +s) +2 (pq +pr + ps +qr +qs +rs)+ 4(pqr +pqs +prs + qrs)+ 8 pqrs
If N = $({{x}_{1}}^{n1})\,({{x}_{2}}^{n2})…..({{x}_{k}}^{nk})$ then number of such pairs are $=\sum\limits_{n=1}^{k}{\left( Tn \right)}$ here$Tn=\left( {{2}^{n-1}} \right)\,\{\,\sum\limits_{n=1}^{n}{x1.x2.xn}\}$
Or to further generalize it number of such pairs is $\sum\limits_{n=1}^{n}{\left[ \left( 2n-1 \right)\left\{ \sum\limits_{n=1}^{n}{x1.x2.xn} \right\} \right]}$

Assignment:

Questions:

1: In how many ways can you express 216 as a product of two of its factors?
a) 4
b) 6
c) 8
d) 9

Solution:
Step 1: 216 = 2333
Step 2: Number of factors are (3+1)(3+1) = 4 x 4= 16
Step 3:  So number of ways ½ (4)(4) = 8

2: Find the sum of factors of 216.

a) 950
b) 850
c) 600
d) 1000
Solution:
Step 1: 216 = 2333
Step 2: Sum of factors = (20+21+22+23)(30+31+32+33)= 15 x 40 = 600

3:In how many ways you can write 200 as product of two of its co-prime factors.
a) 1
b) 2
c) 4
d) 8

Step1: 200 = 2352
Hence number of ways are 22-1 = 2.Infact we can mention these cases as well 8 x 25,
1 x 100.

4: In how many ways you can write 10890 as product of two of its co-prime factors.
a) 7
b) 3
c) 31
d) 15


text:
# Generating Signatures of Tree Structures using an Order-Independent Hash

I'm working with a file format which stores data as a tree. Each node in the tree is known as a Tag, and every tag has a name and a value. The order of tags is not guaranteed, but the general structure is. For example, in the diagram below, the CompoundTag "John Smith" and "Albert Einstein" have the same structure even though their child tags are in a different order:

Formatted as: [TagType] TagName : TagValue

[ListTag] Employees :
[CompoundTag] :
[StringTag] FirstName : John
[StringTag] LastName : Smith
[IntTag] EmployeeId : 123
[CompoundTag] :
[StringTag] FirstName : Albert
[IntTag] EmployeeId : 456
[StringTag] LastName : Einstein

I'm creating signatures of certain tags so that I don't have to parse the whole tree structure, as I'm only interested in specific tags in the tree. To do that, I've created an order-independent hash function which hashes the name of each tag and the type. The function then combines those hashes into one signature which I use to identify other tags with the same structure.

The function hashes tag names using the standard String.hashCode() method, then adds the tag Id (signifying the type) to the upper bits of the hash. It then combines the hash with two previous values which keep track of hashes combined using two commutative operations: addition and exclusive or. I keep track of two combined hashes because I found that addition and xor on their own are easily susceptible to collision. Finally, I combine the two hashes as a long and return that as the tag signature.

I have an optional parameter to include the parent tag in the signature. The parent tag is hashed the same way as child tags are, but I also add a small constant to the parent tag hash to make it slightly different from child tags. I add the constant in-case a child tag has the same name and type as the parent, which would generate the same signature in some scenarios.

The code targets Java 7, though I'm considering switching to Java 8 in the near future. The function itself is as follows:

/*
* Generate an order-independent signature of a given tag.
* If parentTag is specified, it'll include the parentTag
* in the signature.
*
* @param rootTag  is the tag currently being hashed
* @param parentTag  is an optional parent to the rootTag
* @return  the signature for the given tag
*/
public static long calculateTagSignature(Tag tag, Tag parentTag) {

// If the tag is a List/Compound tag, add immediate child tags to stack
// Doesn't add tags deeper than 1 level down
switch (tag.getType()) {
case COMPOUND:
// ArrayList of tags
for (Tag child : ((CompoundTag) tag).getValue()) {
}
break;
case LIST:
// Array of a single tag type
for (Tag child : ((ListTag) tag).getValue()) {
}
}

// Keep track of two hashes using two commutative methods
long sigA = 0; // stores hashes combined using addition
long sigB = 0; // stores hashes combined using xor
long hash; // temporary variable which stores computed hashes

// If parentTag is specified, include it in the signature
if (parentTag != null) {
// Hash the name of the tag
// Parent tag should be hashed different from child tags
hash = 0xD0EF + ((long) parentTag.getName().hashCode()) & 0xffffffffL;
hash += (long) (parentTag.getType().id << 24); // Add tag type to upper bits

// Combine the hash with the two previous hashes
sigB ^= hash; // XOR
}

// Iterate over all child tags
while (!stack.isEmpty()) {
Tag current = stack.remove();

// Hash the name of the tag
hash = ((long) current.getName().hashCode()) & 0xffffffffL;
hash += (long) (current.getType().id << 24); // Include tag type in the upper bits

// Combine the hash with the two previous hashes
sigB ^= hash | (hash << 32); // XOR
}

// Print out the two input signatures for testing
//System.out.format("Add Hash: %X, XOR Hash: %X%n", sigA, sigB);

// Combine the two hashes to generate a full signature based on sigA and sigB
// Magic constant is just a series of randomly generated bits
long signature = sigB + 0xD1D89955F542E43AL + (sigA << 28) + (sigA >>> 2);

return signature;
}

I'm wondering if this is one of the simplest ways to generate an order-independent hash of these objects with few collisions, or if there is a dramatically better approach.

• Nitpick: there's a typo in your data. It's Einstein, not Einstien. – Mast Jan 31 '17 at 20:21
• Is it possible to add everything in a Set, and rely on the hashing of that instead of trying to compute it manually? The Set would also be order independent. – Hesham Attia Jan 31 '17 at 20:36
• @HeshamAttia The hashCode function for Tag currently computes the hash using the name, type, and value, which is necessary for other areas of the program. Thus if I were to use a Set to compute the signature, the computed signature would include the tag values as well. I could add a second hash function to the Tag class which hashes only the name and type and override the Set's hashCode function to call it that function, but that'd essentially amount to computing the hash manually anyway. – jocopa3 Feb 1 '17 at 4:52

Overall, this is very good code. Maybe there is another hashing strategy to have, but I'll focus on the current code in this answer. There is one point that can be greatly improved here: the comments. There are certainly a lot of them, but...

sigA += hash; // ADD
sigB ^= hash; // XOR

or

// Iterate over all child tags
while (!stack.isEmpty()) {

are not really useful. The method is actually quite complicated, with magic numbers, additions and bit-shifting left and right, which lots of people struggle with alone. Instead of those small one-line comments that consider just a single line of code, I would suggest having a long comment on top of it explaining what that particular algorithm, as a whole, is doing and why it is done this way. It could even be part of the Javadoc.

There are two cases: either the person reading those comments (like the ones above) already know what the line is doing, and the comment is not really useful, or they don't, and the comment doesn't really explain why that line of code is there, what its purpose is, and ends up being not really useful as well.

Still, some of those are useful, like

// Parent tag should be hashed different from child tags
hash = 0xD0EF + ((long) parentTag.getName().hashCode()) & 0xffffffffL;

It does more than saying it hashes something. It explains why there is a constant added to the hash: to make it different from hashes of child tags. It could be even better by explaining why that particular constant was chosen; otherwise the reader is thinking: "Was that chosen randomly? Or is it one of those magical constants that make everything work?"

A couple of other notes:

• That part of the code looks like it could use some OOP:

switch (tag.getType()) {
case COMPOUND:
// ArrayList of tags
for (Tag child : ((CompoundTag) tag).getValue()) {
}
break;
case LIST:
// Array of a single tag type
for (Tag child : ((ListTag) tag).getValue()) {
}
}

Perhaps it doesn't make sense with regard to how the rest of the code is structured, but you could consider adding a getValue() abstract method to Tag, perhaps defaulting to return an empty list or without defaults. The idea is that it could make the code be refactored to

for (Tag child : tag.getValue()) {
}

without any switch statement: some tags would return an empty list, and compound or list tags would return their children. If Tag cannot have a getValue() method because it doesn't make sense for all tags to have a value, perhaps another possibility would be to make a specialized subclass/subinterface of Tag for which having a value would make sense, and let the method take that instead. Of course, it could be the case that it would be too much for that lone piece of code.

• I wouldn't let a temporary variable have a broader scope than it should have because it could be reused somewhere else.

long hash; // temporary variable which stores computed hashes

Generally speaking, variables should have the tighter scope possible. In this case, I would just declare two hash variables inside the two for loops. I don't think it would really hurt performance, and it prevents that variable from leaking to other parts of the code.

• Thanks for the suggestions. I can change the ListTag to return an ArrayList of tags so that I don't need two for-loops. The switch statement is still necessary as Tag is defined as public abstract class Tag<T> where T is used as the return type for getValue(), and changing that would cause more headaches elsewhere. As for comments: since this is part of a personal project and not part of a production, I'm not too worried as long as I can read it; though I should practice writing clear and concise commenting regardless, so I'll take your tips into consideration. – jocopa3 Feb 1 '17 at 4:33
• @jocopa3 I didn't realize Tag was a generic class. Then, in the code, it is always used as a raw-type so you should consider using a proper type. – Tunaki Feb 1 '17 at 8:59


text:
# Advantage of MLE over LS or other estimator

Is there any advantage of a maximum-likelihood estimator over another estimator (for example least squares) assuming both methods of estimation are efficient, unbiased, and consistent?

I have been taught that maximum-likelihood methods are generally superior but I don't know why.

• Because the competing estimators might not be efficient and asymptotically unbiased. – JohnK Jul 17 '17 at 15:22

The MLE is more efficient when the distributional assumptions are correctly specified. For instance, if the linear model satisfies, $Y = b X + \epsilon$ where $\epsilon$ comes from, say, a shifted 0 meanWeibull distribution, the MLE will account for the skewness of the errors and the LS will not. It happens to be the case that when $\epsilon$ is normally distributed, you arrive at the least squares estimator anyway.


text:
# Dispersalloy 1 Spill Fast 400mg 50/Jar

Dentsply
\$98.95

Physical Description
Composition:

Silver . . . . . . . . . 69.3%
Tin . . . . . . . . . . . 17.9%
Copper . . . . . . . 11.8%
Zinc . . . . . . . . . . 1%
Mercury . . . . . . . 50%

The world’s most widely used alloy. With over 30 years of proven clinical performance in millions of restorations, Dispersalloy® alloy is the world’s most widely used alloy. Its consistent mix of lathe-cut particles and silver-copper eutectic spheres virtually eliminates the Gamma 2 phase, yielding exceptional corrosion resistance and reduced ditching. And thanks to its patented self-activating capsules, no activation tool is needed.

Current stock 0

## Description

Physical Description
Composition:

Silver . . . . . . . . . 69.3%
Tin . . . . . . . . . . . 17.9%
Copper . . . . . . . 11.8%
Zinc . . . . . . . . . . 1%
Mercury . . . . . . . 50%

The world’s most widely used alloy. With over 30 years of proven clinical performance in millions of restorations, Dispersalloy® alloy is the world’s most widely used alloy. Its consistent mix of lathe-cut particles and silver-copper eutectic spheres virtually eliminates the Gamma 2 phase, yielding exceptional corrosion resistance and reduced ditching. And thanks to its patented self-activating capsules, no activation tool is needed.


text:
20 March 2014

(You might want first see the introduction to this series of posts if you jumped in here randomly.)

## Deep dive

In previous posts I’ve discussed about what are spot instances and what is the spot market and what you can use spot instances for and how. In this post I’m going to write out my thoughts on what is the reason for spot market, its rationality and where actually do spot instances come from.

### Purpose of the EC2 spot market

Why does spot market exist in the first place?

Spot instances were announced on December 14th, 2009. After that there has been several technical updates that brought spot instances to the same level as other instance types (such as EMR support, VPC support). There has been two major published changes on the spot market itself. First, the spot market price algorithm was changed on July 1st 2011 and secondly a default bid price cap was introduced late 2013. These are the visible changes that have the name “spot instance” on them.

What does this tell us about the purpose of the spot market?

Not yet much. But it is telling us something:

• The spot market is meaningful to AWS.
• AWS wants us to use spot instances.

But what about the purpose? Why did AWS go to the complication of providing spot instances (more code, more work, more bugs) and operating a spot market (apparent loss of pricing control) on top of that? Why didn’t it just say “spot instances at 50% price of regular ones” and leave it at that?

I have not seen that AWS would have directly stated the purpose of spot instances. All of the official information I’ve seen carefully skirts about the purpose of spot instances and spot market. The initial announcement tells that “[you can] bid on unused Amazon EC2 capacity” and the current spot instance landing page that “you simply bid on spare Amazon EC2 instances”. There are plenty of whys for the customer, but no why for AWS itself.

I believe the groupthink of the Internet is mostly in line with the following hypothetised (aka naive) purpose:

Spot market is for AWS to sell excess capacity to make at least a bit of more money out of resources that otherwise would remain unused (incurring both operational and capital costs).

This seems sensible and straightforward. Yet it does not tell about the purpose of a spot market. Dmitriy Samovskiy makes a good point about that — why are they “spot” instaces and not “discounted” instances? It is entirely possible that AWS would have priced “discounted instances” at -50% and left it at that (adding the “may-be-terminated-at-any-time” clause). Instead the spot market exist, with its high price volatility, spot price differences between regions and a potential to pay up to $999.99/hour per instance. All of this is bound to make a lot of people wary of spot instances. Think about it. If prices were set at a fixed 50% (or 40%, or 30%) then the element of market variability would be removed. I think a lot of people would be more comfortable with fixed discounts over the variability of spot market prices. There’s this thing called “efficient market hypothesis” in finance theory that posits that financial markets are “efficient” at setting prices on traded assets. That is, the public price reflects supply and demand in a true manner. So one possibility is that maybe — maybe AWS thinks that it can increase its income on spot instances by letting “the market” decide instaneous spot prices instead of a fixed discount? I wouldn’t trust that. After all, the spot instance market is not a real market. Bids are not open. Supply is hidden. Even the pricing algorithm is unknown — assumptions about it being a true bidding market have been shown to be false in the past. (All of this this and more has been pointed out years back in blogosphere as well as in academia). So what is then the purpose of the spot market? I don’t know. I am sure that part of its purpose matches the naive assumption — it is generating income for AWS that otherwise would have been lost. Later below I’ll talk about other partial purpose (that surprisingly ties spot market to reserved instances), but I’m not sure about that being the totality either. In the end I don’t know what is the purpose of the spot market. I’m not saying that it wouldn’t be useful. After all, you can get substantial savings on operational cost using spot instances! You don’t have to theoretize about the purpose of rain to benefit from it, either (in case you’re a farmer). I just don’t believe that the naive hypothesis is all there is to. ### Is the market rational? The answer is absolutely clear and simple for this one: yes and no. See both above and earlier post. The spot market pricing algorithm is not known. I’m not going to call any market rational whose price is potentially set by a random number generator and the market players are finding causality in places where there is none. Yet if you make the assumption that the spot market price is at least mostly a market-driven proxy of supply and demand (and leave the algorithm in the hands of the benevolent AWS) and ask questions about the behavior of the bidders, then the answer is yes. Yes, at least most of the bidders are making rational choices. The question of AWS spot market’s rationality is a common question (see here, here and here). Although the famous$999.99 spike was probably a genuine human mistake (e.g. not rational), it is still useful to ask why anyone would bid over the price of an on-demand instance. A lot of people think it is not logical. Yet this does really occur all the time. Is the net full of loonies or not?

(Yes it is. But let’s consider the spot market only, shall we?)

Earlier I’ve pointed out that the total costs of running a spot instance can easily be less than the cost of using equivalent on-demand instance even when you bid at 10x the on-demand price. Thus at least for those cases where you can expect (based on past history, which of course is not indicator of future blah blah blah) likely savings with high bids then it is entirely rational to bid at >1x prices.

But then what about c3.2xlarge in us-east-1? See the graph below:

Although not we have the benefit of hindsight, I think anyone bidding for c3.2xlarge during January 2014 would have quickly realized that they are not getting an instance at on-demand prices. Why then?

The c3 class of instances was announced in November 2013 and from the very beginning demand for them was high. In fact, demand was higher than supply. For anyone familiar with economy 101 this is a case of supply vs. demand where the price of a good should rise when demand is higher than its supply. Yet the on-demand instance pricing is [not elastic](http://en.wikipedia.org/wiki/Elasticity_(economics) and cannot be changed rapidly by AWS to match the unexpected demand (AWS can change the price, but I don’t think they want to increase the price for PR reasons).

You can see where this is going, right? Spot market price is elastic, and in this case it clearly shows that when demand outstrips supply, the per-unit price increases. In the graph above you can see that c3.2xlarge prices have started to fluctuate and on average, have gone down since late February. This is most likely due to AWS being able to introduce capacity faster than the demand has increased. (An alternative interpretation is that a lot of those interested in c3.2xlarge have become disillusioned at its (un)availability and gone elsewhere.)

But why would anyone pay >1x cost for an instance? There are, after all, plenty of other instance types (even in the c3 class) that are available at on-demand prices from either on-demand or spot markets. Why?

I have no idea what goes in bidder’s heads. But there are a few possibilities that are entirely rational that come to my mind:

• Someone values uninterrupted service over savings. See BrowserMob’s bidding strategy at 4:00 in this video. They clearly put a large weight into getting the resources now even at higher price than later and cheaper.

• Someone tests how their application runs on c3 instances. They might be contemplating moving a production environment over (c3 is cheaper than m1… at regular prices). Doing a time-limited performance evaluation even at the overpriced spot market prices isn’t going to break your bank and would provide you with valuable information for the future (e.g. will you buy c3 reserved instances or not).

I’m sure there are others, but eventually they all boil to the same conclusion: buying at high cost in the spot market is rational if doing so offers larger potential benefits than waiting to buy at regular prices later.

### Spot market price drivers

Although we don’t know the actual spot price algorithm, it is possible to observe it and see whether its behavior correlates with other, known events.

When talking about the spot market algorithm the first stop most definitely has to be a paper called Deconstructing Amazon EC2 Spot Instance Pricing by Beh-Yehuda et al (2011). The researchers did a very thorough analysis of AWS spot instance market and the spot price behavior. Even though most of the analysis is using data prior to the 2011 pricing mechanism change and thus is not valid today it is still a good read. Especially the bit in epilogue where the researches state that

“While these radical qualitative changes [June 2011 pricing mechanism change] are further evidence of the former prices being artificially set, the October prices are consistent with a constant minimal price auction, and are no longer consistent with an AR(1) hidden reserve price.”

So … AWS didn’t use a “market” algorithm before, but they seem to be using one today. As a working hypothesis I’ll take it that there is some market-based price algorithm that takes some inputs and outputs a spot instance price. What are the inputs?

• One thing that we know is that “the Spot price will raise when our [AWS] capacity lowers.” and “the increase in the m2.2xlarge Spot price today [the $999.99 price spike event] was related to an sudden increase in demand for On-Demand m2.2xlarge instances which significantly depleted the unused capacity.” (source). Available EC2 instance capacity affects spot price. • From looking at the c3.2xlarge spot price graph it should be also obvious that demand has an effect. When considering also quotes above it is possible to infer that: Demand for EC2 instances affects spot price. • There is also a *minimum bid price* set by AWS. If you try to bid below this you'll get a price-too-low error with a message *"Your Spot request price of 0.02 is lower than the minimum required Spot request fulfillment price of 0.403."* (numbers naturally vary). **There is a minimum spot price**, which varies by instance type and region. (I am not sure about zones.) (Updated 2014-03-25) Wrong wrong wrong! The price-too-low message is really only talking about current spot price. My bad. However there still appears to be minimum spot prices which I go through in the next post in this series. • AWS has a default maximum bid limit of 4x on-demand price but this is a soft limit and can be raised or removed. The maximum relative bid price varies, but in the data set I have there are several instane types with >50x spot prices. This implies that there are bids at that level and potentially higher. It is not known whether there is any spot price upper limit. (If anyone is brave enough to do a short bid at 10000x price level I’m interested in hearing about the results.) In the next section I’ll talk about my hypothesis about where spot instance capacity comes from, but from Dave@AWS’s quotes and other observations it should be clear that spot market price is affected by demand on all instance types, including on-demand and reserved instances. That is, an increased demand for on-demand instances may affect spot market price even when no changes occur in the spot market bid pool. It seems reasonable to assume all of the above affect prices. But this doesn’t remove the possibility of other price drivers. It is entirely possible that AWS would artificially push the spot price up (to maximize their profits — 50x0.99 is more than 100x0.20) or depress the price (to make spot instances more appealing?). Or an increase in the capacity is fed to the pricing engine slowly to prevent rapid price fluctuations. Or a decrease in capacity is pre-factored so that it is removed in steps instead of a large drop (and matching rapid price increase). Or … ### Where do spot instances come from? Note: Most of this section is pure speculation. I am presenting a hypothesis about AWS’s division of instance resources which may be completely wrong. However as far as I’m concerned it is a hypothesis that is in line with actual observations. The official statement from AWS is that the capacity for spot instances is “spare Amazon EC2 instances” (source). A bit more verbose is Dave@AWS’s commentary in the AWS forums: “To answer your second question, you asked what other capacity pools could be a part of Spot. Behind the scenes, our goal is to have all of Amazon EC2’s unused capacity integrated into Spot. By optimizing the use of these instances, we hope to be able to pass along more savings over time to our customers. Selling our unused capacity means we may leverage unused capacity from other pools like On-Demand or other parts of our capacity that can be temporarily sold but may need to be reclaimed at a later time. It would take precedence over On-Demand, because we do not have the ability to reclaim On-Demand instances, so they cannot be sold there.” (Emphasis is mine.) Let me go through behaviors associated with the main instance types: (Notice how reserved instances and spot instances are complementary to each other.) Although AWS may have other (internal?) capacity pools with other access constraints, I think that “unused” capacity at any moment can be divided into two sets: one that can be used for on-demand instances and one that cannot be used for on-demand instances. This is because of the semantics of reserved instances. “Reserved Instances provide a capacity reservation so that you can have confidence in your ability to launch the number of instances you have reserved when you need them.” (source) When you purchase a reserved instance you have no obligation to run it, but AWS has an obligation to provide you with a reserved instance any time you want to run it. This means that any reserved instance that is not running could be sold, but not as an on-demand instance since AWS cannot evict an on-demand instance at will. See the figure below (which doesn’t have too many non-negations): At any moment in time AWS’s total capacity is split into running instances and unused capacity. Running instances are further divided by type into reserved instances, on-demand instances and spot intances. The pool of unused capacity has a portion which cannot be sold as on-demand instances (because if it was sold and a lot of powered-off reserved instances were started it might not be able to provision resources for all those reserved instances). Thus there is unused capacity that can only be sold as spot instances. This also means that there can be unused spot instance capacity even when on-demand instances cannot be provisioned. So finally we can explain why c3.2xlarge instances could be purchased from spot market even when you couldn’t buy on-demand instances: there was a pool of c3.2xlarge reserved instances already sold that were not powered on. When reserved instances are powered on I think this is what happens: 1. If there is unused capacity, it is used to provision the reserved instance. End of story. 2. If no unused capacity was available, the spot market is notified that it needs to release capacity from the spot pool. 3. Spot market algorithm recalculates the spot price based on the new (reduced) total capacity. If this changes the spot market price then it’ll terminate those spot instances whose bid price fell below the spot market price. The released instance capacity is allocated back to the reserved instance. What happens if the change of the capacity does not change the spot price? I’m not sure. It might be that the spot market algorithm will forcefully increase the spot price. As well it might not. The exact wording from AWS is “If the Spot price exceeds your max bid or there is no longer spare EC2 capacity in a given Spot pool, your instances will be terminated.” which I think leaves open the possibility that a spot instance is terminated also on capacity decrease even when the bid price doesn’t change. If anyone has had their spot instance terminated even when the bid price equals spot price I’d be delighted to hear about your experiences. (It is possible that there are also other pools of resources that are available for spot market use. Maybe new servers are first assigned to a “burn-in pool” which is sold only via the spot market. Maybe AWS has internal testing pools that are available for customers when not needed. I have not seen anything that would suggest so, though.) ### Market efficiency and reserved instances If my hypothesis about that powered-off reserved instance capacity is sold in the spot market then (I claim) that spot market is essential for AWS to maximize its income from reserved instances. You could say that a working spot market is a requirement for reserved instances. Think about it. If AWS was not able to resell unpowered reserved instances then it would be making loss with reserved instances. The c3.8xlarge light usage reserved instance upfront cost is$2666. It has 32 (virtual) cores and 60 GiBs of memory so I think that c3.8xlarge represents almost a physical single server (each E5-2680v2 has 20 threads, which I guess maps to an EC2 core) and I’m pretty sure it’ll cost more than $2666. If AWS was not reselling unpowered reserved instance capacity then anyone buying a light utilization reserved instance would most likely end up costing AWS concrete and real money. At the minimum it would make the gross margin on those physical servers very low. ### Open questions I have a hypothesis. Good hypotheses can be tested with tests that either falsify the hypothesis or give results that are in line with earlier predictions. Actually, to be accurate, I have two hypothesis. The first one is that spot market price is affected by supply and demand for all types of EC2 instances (also that there is a minimum spot price and there is no maximum spot price but we know the first for a fact and I’m not sure the second one is meaningful to explore at all). The second one is that unused but purchased reserved instance capacity is re-sold as spot instances. I’ll infer from these that the spot market should behave in the following manner (all of these apply to each region, instance type and availability zone separately): • Unpowered reserved instances and stopped on-demand instances should not affect spot price. • Purchasing reserved instances (without powering them on) … • … should not affect spot price. • … should decrease unused on-demand instance capacity. (This of course may not be visible in any way.) • … should increase unused spot instance capacity. • Powering reserved instances on … • … may increase spot price. • … may cause spot instances to be terminated (even when spot price remains unaffected). • … should not affect availability of on-demand instances. • Powering reserved instances off … • may decrease spot price. • Provisioning new on-demand instances or starting stopped on-demand instances … • … may increase spot price. (We already know via AWS forum comments about the$999.99 spot price spike that demand for on-demand instances can affect spot market prices. It is not clear what the mechanism here is though — does AWS preferentially give capacity to on-demand requests?)
• … may cause spot instances to be terminated. (See previous.)
• Terminating on-demand instances or stopping on-demand instances may decrease spot price.

That’s a lot. How could these be tested? First and foremost, testing any of these is potentially expensive as you need to provision instances and put in bids for spot instances and you’ll need to pay up for all of that. (These might also violate AWS’s Acceptable Use Policy.) It might be possible to infer some of this data from actual spot market logs and/or other monitoring data, though how I don’t know.

• Buy one or more reserved instances. Start reserved instances. Based on the hypothetised behavior this should cause the spot market price to either increase or remain the same.

(Given that there are other people powering instances on and off this would show up only as a statistical result from many iterations. This applies to all other tests too.)

• Power off reserved instances. This should cause the spot market price to decrease or remain the same.

• Purchase spot instances at spot market price or slightly above. See how often their termination is associated with spot price increases. (Some of them should not be.)

• Purchase spot instances at spot market price. Power on reserved instances. There should be a correlation between starting your reserved instances and termination of your spot instances.

• Start on-demand instances. This may be correlated with spot price increase.

• Stop on-demand instances. This may be correlated with spot price decrease.

Of course testing any of these is fraught with difficulty. Starting and stopping one instance is unlikely to affect the spot market price (the system would have to be near a transition point) and any result could be swamped badly by random effects (other users). You could reduce environmental noise by choosing relatively unused region, zone and instance type but in that case you’d probably have to purchase a significant number of instances to see any effect.

### How does this affect you?

Not really much. You shouldn’t try to second-guess future behavior of spot prices.

If my hypothesis is correct, then you might want to keep in mind that the spot market price is affected by events that occur outside the spot market. That is even an apparently stable market can change suddenly without any change in the bidding pool.

But you already knew that spot market is volatile, didn’t you? No new news, then.

Here’s the next post in the series.


text:
Timezone: »

Poster
Connecting Optimization and Regularization Paths
We study the implicit regularization properties of optimization techniques by explicitly connecting their optimization paths to the regularization paths of corresponding'' regularized problems. This surprising connection shows that iterates of optimization techniques such as gradient descent and mirror descent are \emph{pointwise} close to solutions of appropriately regularized objectives. While such a tight connection between optimization and regularization is of independent intellectual interest, it also has important implications for machine learning: we can port results from regularized estimators to optimization, and vice versa. We investigate one key consequence, that borrows from the well-studied analysis of regularized estimators, to then obtain tight excess risk bounds of the iterates generated by optimization techniques.