Found 33676 Articles for Programming

C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid

Arnab Chakraborty
Updated on 02-Mar-2022 10:56:05

155 Views

Suppose, we are given a grid of dimensions h * w. Every cell in the grid has a specific value assigned to it. We have to maximize the cells having an even value. To do that, we can select a cell that has not been selected before, and then decrease the value by 1 of the current cell and increase the value by 1 of another cell that is located vertically or horizontally adjacent to the current cell. We print out the number of operations and the cell numbers of the increase and decrease operations. The output will be in ... Read More

C++ Program to find out the maximum amount of profit that can be achieved from selling wheat

Arnab Chakraborty
Updated on 02-Mar-2022 10:23:18

367 Views

Suppose, there is n number of cities that are connected with m roads. The roads are unidirectional, the roads can only go from source to destination and not the opposite. The roads are given in the array 'roads' in format {source, destination}. Now, in the cities, wheat is sold at different prices. The price of wheat across the cities is given in an array 'price', where the i-th value is the price of wheat in the i-th city. Now, a traveler can buy wheat from any of the cities and can reach any of the cities (if it is permissible) ... Read More

Return the infinity Norm of the matrix in Linear Algebra in Python

AmitDiwan
Updated on 02-Mar-2022 10:31:45

971 Views

To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy. The 1st parameter, x is an input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned. The 2nd parameter, ord is the order of the norm. The inf means numpy’s inf object. The default is None.The 3rd parameter axis, if an integer, specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies ... Read More

Return the Norm of the vector over given axis in Linear Algebra in Python

AmitDiwan
Updated on 02-Mar-2022 10:27:42

145 Views

To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy. The 1st parameter, x is an input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned. The 2nd parameter, ord is the order of the norm. The inf means numpy’s inf object. The default is None.The 3rd parameter axis, if an integer, specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies ... Read More

C++ program to find out the number of ways a grid with boards can be colored

Arnab Chakraborty
Updated on 02-Mar-2022 10:17:54

391 Views

Suppose, we are given a grid that has 2 rows and n columns. The grid has to be covered by n boards without one board getting over another. Now, the boards have to be colored by any one color between red, blue, and green. Two boards that are adjacent to each other cannot be colored by the same color and if not necessary, all colors do not have to be used. The configuration of the grid is given in the array 'grid', where a particular board in the grid is represented using the same English letter and different boards are ... Read More

Get the Kronecker product of two arrays in Python

AmitDiwan
Updated on 02-Mar-2022 10:13:39

604 Views

To get the Kronecker product of two arrays, use the numpy.kron() method in Python Numpy. Compute the Kronecker product, a composite array made of blocks of the second array scaled by the first.The function assumes that the number of dimensions of a and b are the same, if necessary, prepending the smallest with ones. If a.shape = (r0, r1, .., rN) and b.shape = (s0, s1, ..., sN), the Kronecker product has shape (r0*s0, r1*s1, ..., rN*SN). The elements are products of elements from a and b, organized explicitly by −kron(a, b)[k0, k1, ..., kN] = a[i0, i1, ..., iN] ... Read More

Raise a square matrix to the power n in Linear Algebra in Python

AmitDiwan
Updated on 02-Mar-2022 10:11:10

2K+ Views

To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() in Python For positive integers n, the power is computed by repeated matrix squarings and matrix multiplications. If n == 0, the identity matrix of the same shape as M is returned. If n < 0, the inverse is computed and then raised to the abs(n).The return value is the same shape and type as M; if the exponent is positive or zero then the type of the elements is the same as those of M. If the exponent is negative the elements are floating-point. ... Read More

C++ Program to find out the super vertices in a graph

Arnab Chakraborty
Updated on 02-Mar-2022 10:14:19

276 Views

Suppose, we are given a graph that has n vertices. The vertices are numbered 1 to n, and they are connected by the edges given in the array 'edges'. Each vertex has an 'x' value within a number from 1 to n that is given in the array 'values'. Now, we have to find out the super vertices from the graph. A vertex i is called a 'super vertex' whenever the shortest path from vertex 1 to i doesn't have a vertex with the same 'x' value as the i-th vertex. We print out all the vertices satisfying this criterion.So, ... Read More

Evaluate the lowest cost contraction order for an einsum expression in Python

AmitDiwan
Updated on 02-Mar-2022 10:08:25

184 Views

To get the lowest cost contraction order for an einsum expression, use the numpy.einsum+path() method in Python. The 1st parameter, subscripts specify the subscripts for summation. The 2nd parameter, operands are the arrays for the operation.Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values.In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation operations, by disabling, or forcing summation over specified subscript labels.The resulting path indicates which terms of the input contraction should ... Read More

Tensor contraction with Einstein summation convention in Python

AmitDiwan
Updated on 02-Mar-2022 10:02:12

390 Views

For Tensor contraction with Einstein summation convention, use the numpy.einsum() method in Python. The 1st parameter is the subscript. It specifies the subscripts for summation as comma separated list of subscript labels. The 2nd parameter is the operands. These are the arrays for the operation.The einsum() method evaluates the Einstein summation convention on the operands. Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values.In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation ... Read More

Advertisements