# Matrix Multiplication Algorithm Pseudocode

Abstract— Matrix multiplication is an integral component of most of the systems implementing Graph theory, Numerical algorithms, Digital control, Signal and image processing (i. the first column when multiplied by the matrix. of algorithms in the computational sciences. The booth’s multiplication algorithm is primarily used in computer architectures. Banded Matrix-Vector Multiplication. emphasizes the use of pseudo code in the introductory Computer Science, this approach is to teach students how to first develop a pseudo code representation of a solution to a problem and then create the code from that pseudo code. Pseudocode of the rest of the algorithm: Iterative Matrix Multiplication I'm a bit unhappy with your code, because it's so hard to read tbh. Rotation: We use a generalization of Cannon’s algo-rithm as the primary template. I Strassen's algorithm gives a performance improvement for large-ish N, depending on the architecture, e. Write an algorithm in pseudocode to perform the multiplication of a matrix with a vector. Specifically, an input matrix of size can be divided into 4 blocks of matrices. COMP 250 Winter 2016 1 { grade school algorithms Jan. Consider an NxN complex array. This allows us to exploit fast matrix multiplication. 3 Multithreaded merge sort 797 28 Matrix Operations 813 28. Divide-and-Conquer algorithsm for matrix multiplication A = A11 A12 A21 A22 B = B11 B12 B21 B22 C = A×B = C11 C12 C21 C22 Formulas for C11,C12,C21,C22: C11 = A11B11 +A12B21 C12 = A11B12 +A12B22 C21 = A21B11 +A22B21 C22 = A21B12 +A22B22 The First Attempt Straightforward from the formulas above (assuming that n is a power of 2): MMult(A,B,n) 1. Pseudo Code for Union using Mapreduce. What is the least expensive way to form the product of several matrices if the naïve matrix multiplication algorithm is used? [We use the number of scalar multiplications as cost. spawn is to indicate creation of a new thread. 3) you should encapsulate a matrix into a class, if it is supposed to be c++ (then 2 will be obsolete) 4) your code will be easier to understand (for you as well) if you use better names for x,y,z,i, k and j. Cannon's algorithm: a distributed algorithm for matrix multiplication especially suitable for computers laid out in an N × N mesh; Coppersmith-Winograd algorithm: square matrix multiplication; Freivalds' algorithm: a randomized algorithm used to verify matrix multiplication. 5D (Ballard and Demmel) ©2012 Scott B. The main purpose of this paper is to present a fast matrix multiplication algorithm taken from the paper of Laderman et al. , what the complexity of the problem is). Pseudocode for the algorithm is given in Figure 1. cludes the RSA cryptosystem, and divide-and-conquer algorithms for integer multiplication, sorting andmedianﬁnding, aswellasthe fast Fourier transform. Algorithm Examples! Pseudocode! Order of Growth! Algorithms - what are they Fibonacci - Matrix Multiplication. Multithreaded Algorithms Introduction. To perform the addition, numbers in matching postions in the input matrices are added and the result is placed in the same position in the output matrix. We propose two approaches to matrix multiplication: iter-ative approach and block approach. Unlike standard matrix multiplication, MixColumns performs matrix multiplication as per Galois Field 2 8. Although adjacency matrix representation of graph is used, this algorithm can also be implemented using Adjacency List to improve its efficiency. General Matrix Multiplication (GEMM) is the primary component of the level-3 BLAS and of most dense linear algebra algorithms (and many sparse/structured linear algebra algorithms), which in turn have applications in virtually every area of computational science. Summary I Strassen rst to show matrix multiplication can be done faster than O(N3) time. complexity of matrix multiplication is n2 (2n −1) = 2 ⋅(2 −1)⋅τ T1 n n (8. Matrix multiplication is one of the most fundamental operations in linear algebra and serves as the main building block in many different algorithms, including the solution of systems of linear equations, matrix inversion, evaluation of the matrix determinant, in signal processing, and the transitive closure of a graph. •Pseudo code. Problem 6 (checks whether a matrix is symmetric): yes 4. One of the basic operations on matrices is multiplication. In other words, Lagrees with the corresponding k×nsubmatrix of X. 2 StrassenÕs algorithm for matrix multiplication If you have seen matrices before, then you probably know how to multiply them. Regression algorithm pseudocode from [4]: The regression algorithm follows a nested optimization scheme using coordinate descend. 6 Another Recursive Algorithm 4. If you're interested in typesetting algorithmic code, there are a number of choices. However, due to constant factors and realistic modern architecture constraints, these theoretically faster methods are rarely used; instead, the naive brute force approach to matrix multiplication is that which is. Suppose we want to multiply two n by n matrices, A and B. This analysis culminates in Section 4. The practical beneﬁt from improvements to algorithms is therefore potentially very great. Here's a short example from the algorithmicx documentation (with a pseudocode for loop added):. Assume that square matrix A and B are used for multiplication in the following algorithms. Algorithms { CMSC-37000 Divide and Conquer: The Karatsuba algorithm (multiplication of large integers) Instructor: L aszl o Babai Updated 01-21-2015 The Karatsuba algorithm provides a striking example of how the \Divide and Conquer" technique can achieve an asymptotic speedup over an ancient algorithm. We need to create a Toeplitz matrix using a subsection of a data vector on the device. Matrix Multiplication in Case of Block-Striped Data Decomposition Let us consider two parallel matrix multiplication algorithms. This builds on the previous post on recursive square matrix multiplication. to read the matrix into core memory once [19, sect. So Matrix Chain Multiplication problem has both properties (see this and this) of a dynamic programming problem. To begin with, the sequential algorithm was implemented using the pseudo-code in [2]. Freivalds' algorithm is a probabilistic randomized algorithm used to verify matrix multiplication. 2 Strassen's algorithm for matrix multiplication 4. One key idea in the sorting networks chapter, the 0-1 principle, ap-. x x y matrix by a y x z matrix creates an x x z matrix. The cofactor matrix is the matrix of determinants of the minors A ij multiplied by -1 i+j. Antoine Vigneron (UNIST) CSE331 Lecture 5 July 11, 2017 3 / 19. Write pseudocode for Strassen’s algorithm. r-1 (mod n) where the integers a and b are smaller than the modulus. Complexity Calculation How many additions of integers and multiplications of integers are used by the matrix multiplication algorithm to multiply two n * n matrices. I won't give the pseudo code here for these ones, but they are naive recursive algorithm, bottom up algorithm, naive recursive squaring and recursive squaring. MPI Matrix-Matrix Multiplication Matrix Products Parallel 2-D Matrix Multiplication Characteristics Computationally independent: each element computed in the result matrix C, c ij, is, in principle, independent of all the other elements. 6 Another Recursive Algorithm 4. Provide your analysis for the following problem statement: Write a program that will calculate the results for the multiplication table up to 10x10 in steps of 1 beginning at 1. cient implementation of sparse matrix multiplication on a memory intensive associative processor (AP), verified by extensive AP simulation using a large collection of sparse matrices [41]. We then "combine" the middle row of the key matrix with the column vector to get the middle element of the resulting column vector. The multiplier contains only 0s and 1s,. The algorithms classes I teach at Illinois have two significant prerequisites: a course on discrete mathematics and a course on fundamental data structures. Block matrices are briefly discussed using 2 × 2 block matrices. I have a question for you about your approach. What is a Spanning tree? Explain Prim’s Minimum cost spanning tree algorithm with suitable example. Before going to main problem first remember some basis. This page contains the order of topics contained in lectures, listed as a sequence of modules. Which is faster for this value of n?. To save space and running time it is critical to only store the nonzero elements. i) Multiplication of two matrices ii) Computing Group-by and aggregation of a relational table i) Multiplication of two matrices ii) Computing Group-by and aggregation of a relational table. 2 shows the calculate steps of covariance matrix, mainly including: complex conjugate multiplication between the lines of input matrix, then do an accumulation operation. Idea - Block Matrix Multiplication The idea behind Strassen’s algorithm is in the formulation of matrix multiplication as a recursive problem. You can use a pseudocode environment algpseudocode offered by algorithmicx. SPARSE MATRICES C/C++ Assignment Help, Online C/C++ Project Help and Homework Help introduction A matrix is a mathematical object that arises in many physical problems. 1 The naive matrix multiplication algorithm Let A and B be two n £ n matrices. Matrix mulitplication using Linked List - posted in C and C++: I have to implement Matrix multiplication using singly linked list. Pseudocode of the rest of the algorithm: Iterative Matrix Multiplication I'm a bit unhappy with your code, because it's so hard to read tbh. Material for the algorithms class taught by Emanuele "Manu" Viola. Dynamic Programming—Chained Matrix Multiplication Multiplying unequal matrices • Suppose we want to multiply two matrices do not have the same number of rows and columns • We can multiply two matrices A 1 and A 2 only if the number of columns of A 1 is equal to the number of rows of A 2. Example of Matrix Multiplication by Fox Method Thomas Anastasio November 23, 2003 Fox's algorithm for matrix multiplication is described in Pacheco1. However, this algorithm is infamously inapplicable, as it relies on Coppersmith and Winograd’s fast matrix multiplication. It is referenced specifically in the pseudocode but that is not the only location where it is appropriate to call it. If you are interested in a Modified Gauss-Jordan Algorithm, you can see this. Algorithm for the Transpose of a Sparse-Matrix: This is the algorithm that converts a compressed-column sparse matrix into a compressed-row sparse matrix. The practical beneﬁt from improvements to algorithms is therefore potentially very great. What is the best algorithm for matrix multiplication ? Actually there are several algorithm exist for matrix multiplication. Here, we will discuss the implementation of matrix multiplication on various communication networks like mesh and. - Overall complexity of parallel matrix-vector multiplication algorithm ( n2=p+n+logp) - Isoefﬁciency of the parallel algorithm Time complexity of sequential algorithm: ( n2) Only overhead in parallel algorithm due to all-gather For reasonably large n, message transmission time is greater than message latency. Solutions for CLRS Exercise 4. 2 Algorithmic Techniques 5. \begin{algorithm} \caption{Euclid's algorithm}\label{euclid} \. 1 Naive Matrix Multiplication 4. Matrix-matrix multiplication takes a triply nested loop. I'm just doing a self-study of Algorithms & Data structures and I'd like to know if anyone has a C# (or C++) implementation of Strassen's Algorithm for Matrix Multiplication? I'd just like to run it and see what it does and get more of an idea of how it goes to work. Let us start with a very simple example th. One common practice is to translate convolution to im2col and GEMM, which lays out all patches into a matrix. 84 videos Play all Algorithms Abdul Bari; Derivatives explained - Duration: 10:13. One of the basic operations on matrices is multiplication. The algorithm is called a (7, 4) code, because it requires seven bits to encoded four bits of data. Recurrence equation for Divide and Conquer: If the size of problem ‘p’ is n and the sizes of the ‘k’ sub problems are n1, n2…. Write An Algorithm To Find The Power Of A Number. 2 StrassenÕs algorithm for matrix multiplication If you have seen matrices before, then you probably know how to multiply them. This page contains the order of topics contained in lectures, listed as a sequence of modules. GitHub Gist: instantly share code, notes, and snippets. Adjacency matrix representation of graph where the n X n matrix W = (wij) of edge weights. Matrix multiplication algorithms. Definition of Flowchart A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols, shapes and arrows in order to demonstrate a process or a program. complexity by packing the inner loops into a single matrix product as shown in Algorithm 2. emphasizes the use of pseudo code in the introductory Computer Science, this approach is to teach students how to first develop a pseudo code representation of a solution to a problem and then create the code from that pseudo code. The unit of. Matrix multiplication is the process of taking two or more n x n matrices and calculating a product by summing the product of each row in one matrix with the column of the second matrix. As in case of developing the matrix-vector multiplication algorithm, we use one-dimensional arrays, where matrices are stored rowwise. So assuming that both these multiplication steps are executed every time the loop executes, we see that 2. From Math Insight. We tackle scenarios such as matrix multiplication and linear regression/classification in which we wish to estimate inner products between pairs of vectors from two possibly different sources. The problem is quite easy when n is relatively small. 1 and the Junto. Below is some sample output. It is the technique still used to train large deep learning networks. org) I now want to use strassen's method which I learned as follows:. Unlike general multiplication, matrix multiplication is not commutative. o The number of additions and multiplication's required for this algorithm can be calculated as follows: To calculate one entry in the product matrix, we must perform k multiplications and k-1 additions. for i = 1 to n. 1 and Step 3. We can use simple recursion, f(n) = f(n-1) + f(n-2), or we can use dynamic programming approach to avoid the calculation of same function over and over again. Write a c program to find out transport of a matrix. it explains matrix multiplication. Antoine Vigneron (UNIST) CSE331 Lecture 5 July 11, 2017 3 / 19. In other words, Lagrees with the corresponding k×nsubmatrix of X. Question: Show Map Reduce implementation for the following two tasks using pseudocode. - Explain the difference between an LED and OLED display. matrix multiplication; this means that matrix multiply based methods for determining primitivity cannot be sped up anymore at this time. 3343(abc)(log7)/(3) 2. Block matrices are briefly discussed using 2 × 2 block matrices. x x y matrix by a y x z matrix creates an x x z matrix. Section 5 provides a com-parison with related works. Summary I Strassen rst to show matrix multiplication can be done faster than O(N3) time. In the next three parts, you may be writing pseudocode. That’s very important because for small n (usually n < 45) the general algorithm is practically a better choice. Integer-multiplication, Matrix Multiplication - Strassen Alg You study after every class/week, the syllabus accumulates fast before you know! Aug 26, W (Drop w/o W grade, Aug 28) Dynamic Programming: 0-1Knapsack. This work is licensed under aCreative Commons. parallel before a loop means each iteration of the loop are independant from each other and can be run in parallel. Notes: A common reference for double-precision matrix multiplication is the dgemm ( d ouble-precision ge neral m atrix- m atrix multiply) routine in the level-3 BLAS. The application. Given three n x n matrices, Freivalds' algorithm determines in O(kn^2) whether the matrices are equal for a chosen k value with a probability of failure less than 2^-k. Name the algorithmic technique used. 3 Matrix Multiplication for Banded Matrices. com Free Programming Books Disclaimer This is an uno cial free book created for educational purposes and is not a liated with o cial Algorithms group(s) or company(s). 0 is there to suggest that different values can be used, but they should be related to the number of input variables. The following algorithm multiplies nxn matrices A and B: // Initialize C. Strassen's algorithm, the original Fast Matrix Multiplication (FMM) algorithm, has long fascinated computer scientists due to its startling property of reducing the number of computations required for multiplying. and similarly for the bottom row. We know that, to multiply two matrices it is condition that, number of columns in first matrix should be equal to number of rows in second matrix. Matrix Multiplication: Strassen's Algorithm. What Is The Main Operation Of This Algorithm? C. We need to create a Toeplitz matrix using a subsection of a data vector on the device. of algorithms in the computational sciences. You can call the algorithm on sub-matrices of dimensions n-1 each when the size of the matrix is odd in the recursive step and calculate 2n-1 remaining elements using normal vector multiplication in O(n) each and a total of O(n^2). We tackle scenarios such as matrix multiplication and linear regression/classification in which we wish to estimate inner products between pairs of vectors from two possibly different sources. What is the best algorithm for matrix multiplication ? Actually there are several algorithm exist for matrix multiplication. n and r is relatively prime number to n (gcd (n, r)= 1). for k = 1 to n. Part I was about simple matrix multiplication algorithms and Part II was about the Strassen algorithm. If A is the adjacency matrix of G, then (A I)n 1 is the adjacency matrix of G*. Two groups of algorithms belonging to this class are called the matrix method, and the Wallace-tree method, respectively. 3) where τ is the execution time for an elementary computational operation such as multiplication or addition. Section 5 provides a com-parison with related works. The aim is to get the idea quickly and also easy to read without details. The former is suitable for sparse matrices, while the latter is appropriate for dense matrices with low communication overhead. We could break down the steps as follows. , what the complexity of the problem is). In matrix addition, one row element of first matrix is individually added to corresponding column elements. 4 uses dynamic programming to find an optimal triangulation of a convex polygon, a problem that is surprisingly similar to matrix-chain multiplication. Matrix multiplication is one of the most fundamental operations in linear algebra and serves as the main building block in many different algorithms, including the solution of systems of linear equations, matrix inversion, evaluation of the matrix determinant, in signal processing, and the transitive closure of a graph. Then, user is asked to enter two matrix and finally the output of two matrix is calculated and displayed. That’s very important because for small n (usually n < 45) the general algorithm is practically a better choice. Each matrix Mk has dimension pk-1 x pk. The Floyd Warshall algorithm, itis the algorithm in which there is the use of different characterization of structure for a shortest path that we used in the matrix multiplication which is based on all pair algorithms. Use row communicators and column communicators to scatter and broadcast the vector. We ended up pursuing a different route, but I decided to continue pursuing the problem on my own time. 2x2 Matrix Multiplication Calculator is an online tool programmed to perform multiplication operation between the two matrices A and B. From Math Insight. This work is licensed under aCreative Commons. Which method yields the best asymptotic running time when used in a divide-and-conquer matrix-multiplication. Provide a specification to describe the behaviour of this algorithm, and prove that it correctly implements its specification. However, this algorithm is infamously inapplicable, as it relies on Coppersmith and Winograd’s fast matrix multiplication. The weights and values of 6. Sparse Matrix Multiplication. I am trying to implement a multiplication algorithm by overloading the *= operator. Matrix Multiplication in Case of Block-Striped Data Decomposition Let us consider two parallel matrix multiplication algorithms. 3 Storage formats 3. Other types of algorithms for this problem appear in [15, 16]. In particular, this includes judging which data structures, libraries, frameworks, programming languages, and hardware platforms are appropriate for the computational task, and using them effectively in the implementation. How do they differ? - Is a pixel a little square? If not, what is it? What implications does this have? Give at least 2. One of the basic operations on matrices is multiplication. It is the technique still used to train large deep learning networks. Then, we'll present a few examples to give you a better idea. Floyd Warshall. What Is The Main Operation Of This Algorithm? C. For instance, the algorithm we're interested in looking at, Dijkstra's algorithm, only works if none of the edges on the graph have negative weights -- the "time" it takes to traverse the edge is somehow less than 0. emit (key, result). GATEBOOK Video Lectures 3,203 views. These lectures were designed for the latter part of the MIT undergraduate class 6. Matrix Multiplication Problem - Duration: 27:38. - Overall complexity of parallel matrix-vector multiplication algorithm ( n2=p+n+logp) - Isoefﬁciency of the parallel algorithm Time complexity of sequential algorithm: ( n2) Only overhead in parallel algorithm due to all-gather For reasonably large n, message transmission time is greater than message latency. Other types of algorithms for this problem appear in [15, 16]. • Algorithms are step-by-step procedures for problem solving • They should have the following properties: •Generality •Finiteness •Non-ambiguity (rigorousness) •Efficiency • Data processed by an algorithm can be • simple • structured (e. Strassen's method of matrix multiplication is a typical divide and conquer algorithm. Although we won't describe this step in detail, it is important to note that this multiplication has the property of operating independently over each of the columns of the initial matrix, i. Matrix Multiplication; Matrix Multiplication Parenthesization; Brute Force Solution: Try all possible parenthesizations; Dynamic Programming Solution (4 steps) Step 1: Characterize Structure of Optimal Solutioon; Step 2: Define recursive solution; Recursive Solution; Analysis; Duplicate Subproblems; Unique Subproblems; Step 3: Bottom-Up Approach; Dynamic Programming. 5 Maximum Flow. Matrix Multiplication c c2 1= r2 A1 A2 r1 r2! = r1 ! c2 (r1 ! c2) ! c1 = multiplications If r 1 = c 1 = r 2 = c 2 = N, this standard approach takes ( N3): I For every row ~r (N of them) I For every column ~c (N of them) I Take their inner product: r c using N multiplications 2. listing algorithms. Idea - Block Matrix Multiplication The idea behind Strassen's algorithm is in the formulation of matrix multiplication as a recursive problem. Describe how an array can be effectively used to store a sparse matrix. Matrix mulitplication using Linked List - posted in C and C++: I have to implement Matrix multiplication using singly linked list. You don't need multiplication facts to use the Russian peasant algorithm; you only need to double numbers, cut them in half, and add them up. The unit of. We then "combine" the middle row of the key matrix with the column vector to get the middle element of the resulting column vector. The problem is quite easy when n is relatively small. John, Your comment about matrix multiplication was forwarded to the mahout-user emailing list. emphasizes the use of pseudo code in the introductory Computer Science, this approach is to teach students how to first develop a pseudo code representation of a solution to a problem and then create the code from that pseudo code. Section 3 pro-vides implementation details on our design. In the other hand the algorithm of Strassen is not much faster than the general n^3 matrix multiplication algorithm. 2 shows the calculate steps of covariance matrix, mainly including: complex conjugate multiplication between the lines of input matrix, then do an accumulation operation. This immediately leads to a counting algorithm with running time Θ(n3) respectively Θ(nγ), where γ is the matrix multiplication exponent. Divide-and-conquer multiplication. 2 StrassenÕs algorithm for matrix multiplication If you have seen matrices before, then you probably know how to multiply them. These are lecture notes used in CSCE 310 (Data Structures & Algorithms) at the University of Nebraska|Lincoln. count example presented in Section 2. The matrix M and the vector v each will be stored in a file of the DFS. Pseudo-code for i = 1 : n yi yi +axi Number of ﬂops is 2n Pseudo-code cannot run on any computer, but are human readable and straightforward to convert into real codes in any programming language (e. Matrix multiplication is not commutative, but it is associative, so the chain can be parenthesized in whatever manner deemed best. Purdue University Purdue e-Pubs ECE Technical Reports Electrical and Computer Engineering 9-1-1992 Implementation of back-propagation neural networks with MatLab. Prim's Algorithm Implementation in C++. Write An Algorithm To Find The Power Of A Number. Flowchart for Matrix multiplication : Algorithm for Matrix multiplication :. Strassen's matrix multiplication program in c 11. Program the divide and conquer matrix multiplication using 1) standard algorithm 2) recursion 3) strassen’s method. Write the Pseudo Code of the matrix multiplication program that performs the worst execution time and explain why - Answered by a verified Programmer We use cookies to give you the best possible experience on our website. Sparse matrices, which are common in scientific applications, are matrices in which most elements are zero. The dimensions are stored in array. Using the most straightfoward algorithm (which we assume here), computing the product of two matrices of dimensions (n1,n2) and (n2,n3) requires n1*n2*n3 FMA operations. Our first example of dynamic programming is an algorithm that solves the problem of matrix-chain multiplication. 2 Multithreaded matrix multiplication 792 27. (Otherwise, you should read Section D. When distributing the vector among processors, implement the algorithm shown in Figure (b) on page 22 of lecture notes “ Parallel matrix algorithms (part 2) ”. Summary: The two fast Fibonacci algorithms are matrix exponentiation and fast doubling, each having an asymptotic complexity of $$Θ(\log n)$$ bigint arithmetic operations. The simplest sparse matrix data structure is a list of the nonzero entries in arbitrary order. 1 The SUMMA Algorithm. The introduction of the technique is attributed to a 1962 paper by Karatsuba, and indeed it is sometimes called Karatusba multiplication. We can build a sketch as we scan through the matrix. I am trying to write pseudo code in my paper. I implement these three algorithms from pseudocode from the book to Java code: Merge-Sort Java version：. Animated Algorithms (sorting, priority queues, Huffman, Matrix chain multiplication, MST, Dijkstra) Graph Algorithms (Dijkstra, Prim, Kruskal, Ford-Fulkerson) Java and Web Based Algorithm Animation (JAWAA). What is the main operation of this algorithm? c. Solutions for CLRS Exercise 4. 4 RESULTS We evaluate our implementation by testing its performance on one. com Free Programming Books Disclaimer This is an uno cial free book created for educational purposes and is. There is a faster way to multiply, though, caled the divide-and-conquer approach. 3 perform the multiplication operation. Output Y: d by N matrix consisting of d D dimensional embedding coordinates for the input points. Freivalds' algorithm is a probabilistic randomized algorithm used to verify matrix multiplication. Better asymptotic bounds on the time required to multiply matrices have been known since the work of Strassen in the 1960s, but it is still unknown what the optimal time is (i. In this post I will explore how the divide and conquer algorithm approach is applied to matrix multiplication. Purdue University Purdue e-Pubs ECE Technical Reports Electrical and Computer Engineering 9-1-1992 Implementation of back-propagation neural networks with MatLab. Have you considered doing the multiplication in a single step by storing the the first matrix in column major order and the second in row major order?. Explain why memoization fails to speed up a good divide-and-conquer algorithm like merge-sort. CS 2073 Lab 10: Matrix Multiplication Using Pointers Chia-Tien Dan Lo Department of Computer Science, UTSA I Objectives Show how to manipulate commandline arguments in C Demonstrate your ability to read matrices from les Demonstrate your ability to use pointers for matrix multiplication II Hand-in Requirements. You are given 5 different algorithms for different purposes and their pseudocodes that are listed below. Related Questions More Answers Below. So Matrix Chain Multiplication problem has both properties (see this and this) of a dynamic programming problem. Since we have not covered multiplication yet, a function has been provided to you. Two groups of algorithms belonging to this class are called the matrix method, and the Wallace-tree method, respectively. Matrix multiplication is one of the most fundamental operations in linear algebra and serves as the main building block in many different algorithms, including the solution of systems of linear equations, matrix inversion, evaluation of the matrix determinant, in signal processing, and the transitive closure of a graph. LLE Algorithm Pseudocode (Notes, e. 2 Algorithmic Techniques 5. Find f(n): n th Fibonacci number. 3) you should encapsulate a matrix into a class, if it is supposed to be c++ (then 2 will be obsolete) 4) your code will be easier to understand (for you as well) if you use better names for x,y,z,i, k and j. Application. using matrix multiplication Let G=(V,E) be a directed graph. Book shows pseudocode for simple divide and conquer matrix multiplication: n = A. r-1 (mod n) where the integers a and b are smaller than the modulus. Matrix multiplication of two sparse matrices is a fundamental operation in linear Baye sian inverse problems for computing covariance matrices of observations and a posteriori uncertainties. HOME ; A comparison of numerical approaches to the solution of the time-dependent Schrödinger equation in one dimension. complexity of matrix multiplication is n2 (2n −1) = 2 ⋅(2 −1)⋅τ T1 n n (8. The current best algorithm for matrix multiplication O(n2:373) was developed by Stanford's own Virginia Williams[5]. Pseudocode for the algorithm is given in Figure 1. Make good use of matrix multiplication! It avoids a lot of loops, so it makes your code cleaner and faster! DO NOT assume that I will answer your email questions or posts to the discussion forum after 3pm on Sunday. which can be signi cantly smaller than their dense equivalent. Pseudocode for Matrix Vector Multiplication by Mapreduce. , the shapes are 2 n × 2 n for some n. i) Multiplication of two matrices ii) Computing Group-by and aggregation of a relational table i) Multiplication of two matrices ii) Computing Group-by and aggregation of a relational table. \begin{algorithm} \caption{Euclid’s algorithm}\label{euclid} \. Since we have not covered multiplication yet, a function has been provided to you. Matrix-matrix multiplication takes a triply nested loop. 2 Multithreaded matrix multiplication 792 27. 2 Matrix-Matrix Multiplication. So assuming that both these multiplication steps are executed every time the loop executes, we see that 2. Alternative approaches can be seen as straight forward iteration over the nodes or edges of the graph. In grade 1, you learned how to count up to ten and to do basic arithmetic using your ngers. Integer-multiplication, Matrix Multiplication - Strassen Alg You study after every class/week, the syllabus accumulates fast before you know! Aug 26, W (Drop w/o W grade, Aug 28) Dynamic Programming: 0-1Knapsack. The definition of matrix multiplication is that if C = AB for an n × m matrix A and an m × p matrix B, then C is an n × p matrix with entries = ∑ =. As examples, pseudocode is presented for the inner product, the Frobenius matrix norm, and matrix multiplication. Explain why memoization fails to speed up a good divide-and-conquer algorithm like merge-sort. Deﬁne the meaning of your variables. Can some one please help me to format it. Section 5 provides a com-parison with related works. With the current implementation of the cuBlas functions we need to write kernel code to do this efficiently. The problem is quite easy when n is relatively small. SPARSE MATRICES C/C++ Assignment Help, Online C/C++ Project Help and Homework Help introduction A matrix is a mathematical object that arises in many physical problems. Pseudocode Matrix Multiplication. This relies on the block partitioning which works for all square matrices whose dimensions are powers of two, i. In matrix multiplication, one row element of first matrix is individually multiplied by all column elements and added. Order of both of the matrices are n × n. The word is derived from the phonetic pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al-Khowarizmi, who. Adjacency-matrix and adjacency-list representations Breadth-first and depth-first search using adjacency lists Computing connected components of a graph Strongly-connected and biconnected components Topological sorting Algebraic algorithms: Strassen matrix multiplication algorithm The Four Russians boolean matrix multiplication Winograd's algorithm. Matrix Multiplication Algorithm. Faster matrix multiplication in general is an important applied topic, because it can speed up all sorts of scientific, engineering, and ML algorithms that have it as a step (often one of the bottleneck steps). At the end of the lecture, we saw the reduce SUM operation, which divides the input into two halves, recursively calls itself to obtain the sum of these smaller inputs, and returns the sum of the results from those. Specifically, an input matrix of size can be divided into 4 blocks of matrices. GATEBOOK Video Lectures 3,203 views. 5D (Ballard and Demmel) ©2012 Scott B. Of course, writing pseudocode is child's play compared to actually implementing a real algorithm. In section 4, we discuss our experimental results from the real hardware implementation. return C {C = [cij ] is the product of A and B} 22. Benchmarked it to be 4x faster than the scalar version (on a Pentium M, using GCC 4.


# Math Help - Cube Root times Cube Root

1. ## Cube Root times Cube Root

cube root of 16(m^2)(n) multiplied by cube root of 27(m^2)(n)

I converted the problem to (16m^2n)^1/3 * (27m^2n)^/3.

Is this the correct way to write the original problem another way?

I finally got (432m^4n^2)^2/3.

Is this correct?

2. Originally Posted by sharkman
cube root of 16(m^2)(n) multiplied by cube root of 27(m^2)(n)

I converted the problem to (16m^2n)^1/3 * (27m^2n)^/3.

Is this the correct way to write the original problem another way?

I finally got (432m^4n^2)^2/3.

Is this correct?
Nearly there. Recall that $\sqrt[3]{a} \times \sqrt[3]{b} = \sqrt[3]{ab}$

In your question you have $\sqrt[3]{16(m^2)(n)} \times \sqrt[3]{27(m^2)(n)}$.

Using the fact I gave first, this means than the answer is...

Hint: Your multiplication of the $m$ and $n$ terms was not wrong, it was the index - $\frac{2}{3}$ - that you got wrong.

Originally Posted by craig
Nearly there. Recall that $\sqrt[3]{a} \times \sqrt[3]{b} = \sqrt[3]{ab}$

In your question you have $\sqrt[3]{16(m^2)(n)} \times \sqrt[3]{27(m^2)(n)}$.

Using the fact I gave first, this means than the answer is...

Hint: Your multiplication of the $m$ and $n$ terms was not wrong, it was the index - $\frac{2}{3}$ - that you got wrong.
The answer should be cubert{432m^4n^2}, right?

4. Correct

5. ## great

Originally Posted by craig
Correct
Great! Thanks!

6. Originally Posted by sharkman
The answer should be cubert{432m^4n^2}, right?
Don't be too shocked if your teacher marks it wrong because it has not been simplified as much as it could be. $m^4= m^3 m$ so $\sqrt[3]{m^4}= \sqrt[3]{m^3}\sqrt[3]{m}= m\sqrt[3]{m}$. Perhaps more importantly, $432= (16)(27)= (2^4)(3^3)= (2^3)(3^3)(2)$ so $\sqrt[3]{432}= \sqrt[3]{2^3}\sqrt[3]{3^3}\sqrt[3](2)= 6\sqrt[3]{2}$.

The best way to write your answer is $6m\sqrt[3]{2mn^2}$.

7. ## ok

Originally Posted by HallsofIvy
Don't be too shocked if your teacher marks it wrong because it has not been simplified as much as it could be. $m^4= m^3 m$ so $\sqrt[3]{m^4}= \sqrt[3]{m^3}\sqrt[3]{m}= m\sqrt[3]{m}$. Perhaps more importantly, $432= (16)(27)= (2^4)(3^3)= (2^3)(3^3)(2)$ so $\sqrt[3]{432}= \sqrt[3]{2^3}\sqrt[3]{3^3}\sqrt[3](2)= 6\sqrt[3]{2}$.

The best way to write your answer is $6m\sqrt[3]{2mn^2}$.
I understand. You decided to break the problem down a bit more.


# Prove that $\sim$ is an equivalence relation

Let $$A = \{1, 2, 3,...,9\}$$ and let $$\sim$$ be the relation on $$A\times A$$ defined by $$(a,b) \sim(c,d)$$ if $$a+d = b+c$$.

Prove that $$\sim$$ is an equivalence relation.

Really stuck on this question, maybe its the use of ~ that is throwing me off. Any suggestions on how to do this? Maybe I could swith ~ out for something else while solving then put it back in.

Thanks

• You have to show ~ is reflexive, symmetric, and transitive. Note $a+d=b+c \iff a-b=c-d$ – J. W. Tanner Apr 10 '19 at 21:20
• (1) If the symbol ~ annoys you, simply replace it by the more ordinary symbol R. (2) If you are stuck, it may come from the fact that, in this problem, the "elements" you are deling with are, in fact, ordered pairs. So for example, proving reflexivity amounts to proving that any ordred pair (a,b) has the relation R ( or ~ ) with itself , that is (a,b) R (a,b). Using the defining formula, you could see what you have to prove, and then prove if using basic arithmetic. – Saint James Apr 10 '19 at 21:25
• The symbol "~" is often used to denote a relation in case this relation is an equivalence relation. But, before having proved that it is actually an equivalence relation in this case, you are not allowed to read it as " is equivalent to". So, accordind to me, the best thing do do is to replace it by the ordinary symbol R, while you are doing your proof. – Saint James Apr 10 '19 at 21:35

Clearly, the statement $$a+d=b+c$$ is just saying that $$a-b=c-d$$, i.e. two ordered pairs are similar if the difference of their terms is equal. To show it's an equivalence relation, we need reflexivity, transitivity and symmetry. The first and last are obvious.

For transitivity, we need to show $$x\sim y$$ and $$y\sim z$$ implies $$x\sim z$$. Let $$x=(x_1,x_2),y=(y_1,y_2),z=(z_1,z_2)$$. The conditions tell you that $$x_1-x_2=y_1-y_2$$, but also that $$y_1-y_2=z_1-z_2$$. Clearly then, $$x_1-x_2=z_1-z_2$$. But this is exactly what it means for $$x\sim z$$!

I don't normally do this but I think it may clear some conceptions up. If you want to get to how to effectively solve this skip to the end.

...

Maybe an example first:

If we let $$A = (3,6)$$ then $$A\sim (m,n)$$ if $$3+ n = 6+m$$ so the list of $$(m,n)$$ where $$A \sim (m,n)$$ are the ones where $$n = 3+m$$ so $$\{(1,4),(2,5),(3,6),(4,7),(5,8),(6,9)\}$$ are all the $$(m,n)$$ where $$A \sim (m,n)$$.

We can go a little further.

If $$(a,b) \equiv (c,d)$$ then $$a+d = b+c$$ and $$a-b = c-d$$.

So the "classes" are all the ones related to $$(x_1, x_2)$$ where $$x_1 - x_2 =$$:

$$-8$$: $$\{(1,9)\}$$

$$-7$$: $$\{(1,8),(2,9)\}$$

$$-6$$: $$\{(1,7),(2,8),(3,9)\}$$

$$-5$$: $$\{(1,6),(2,7),(3,8),(4,9)\}$$

$$-4$$: $$\{(1,5),(2,6),(3,7),(4,8),(5,9)\}$$

$$-3$$: $$\{(1,4),(2,5),(3,6),(4,7),(5,8),(6,9)\}$$

$$-2$$: $$\{(1,3),(2,4),(3,5),(4,6),(5,7),(6,8),(7,9)\}$$

$$-1$$: $$\{(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9)\}$$

$$0$$: $$\{(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9)\}$$

$$1$$: $$\{(2,1),(3,2),(4,3),(5,4),(6,5),(7,6),(8,7),(9,8)\}$$

$$2$$: $$\{(3,1),(4,2),(5,3),(6,4),(7,5),(8,6),(9,7)\}$$

$$3$$: $$\{(4,1),(5,2),(6,3),(7,4),(8,5),(9,6)\}$$

$$4$$: $$\{(5,1),(6,2),(7,3),(8,4),(9,5)\}$$

$$5$$: $$\{(6,1),(7,2),(8,3),(9,4)\}$$

$$6$$: $$\{(7,1),(8,2),(9,3)\}$$

$$7$$: $$\{(8,1),(9,2)\}$$

$$8$$: $$\{(9,1)\}$$

All the pairs in each set are related to each other this is an "equivalence" because every pair is is exactly one class and can be interchanged with others in the class with no ambiguity.

A relationship could not be equivalent if some elemtns are in more than one list. Of some elements aren't in any list. Or if such bunch of lists wouldn't make any sense to make.

A relationship is an equivalence if

1) it is reflexive. that is for every pair $$(a,b)$$ then $$(a,b)\sim (a,b)$$ or for every $$(a,b)$$ then $$a-b = a-b$$. That's always true.

If we view it as $$(a,b)\sim (c,d)$$ if $$a-b = c-d$$ then we'd have $$a-b = a-b$$ which is of course true.

2) it is symmetric. For any pair $$(a,b) \sim (c,d)$$ then $$(c,d)\sim (a,b)$$. That is is $$a+d = b+c$$ then $$c+b = d+ a$$. That's ... clear.

If we view is as $$(a,b)\sim (c,d)$$ if $$a-b= c-d$$ then if $$a-b = c-d$$ then $$c-d = a-b$$ so $$(a,b)\sim(c,d)\iff (c,d)\sim (a,b)$$.

3) it is transitive. That is if $$(a,b)\sim (c,d)$$ and $$(c,d)\sim (e,f)$$ then $$(a,b) \sim (e,f)$$.

So if $$a+ d=b+c$$ and $$c+f = d+ e$$ then we have to prove that means $$a+f = b+e$$.

We can do that via arithmetic $$a+d +c+f =b+c + d + e$$ so $$a+f = b+e$$.

Just use the definitions:

For reflexivity, you have to show that for any $$(a,b)$$, it is true that $$(a,b)\sim (a,b)$$, i.e. that $$a+b=b+a$$. Well, $$a$$ and $$b$$ are natural numbers, and those are commutative, so yes, this is true.

Symmetry: here you have to show that if $$(a,b) \sim (c,d)$$, then $$(c,d) \sim (a,b)$$, which is to say: show that if $$a+d=b+c$$, then $$c+b=d+a$$ ... can you show this?

Finally, now that I have shown what you need to prove to demonstrate reflexivity and symmetry, can you figure out what you need to show to demonstrate transitivity?


Question

# The average score of boys in an examination of a school is $$71$$ and that of girls is $$73$$. The average score of the school in that examination is $$71.8$$. The ration of the number of boys to the number of girls appeared in the examination, is

A
3:2
B
3:4
C
1:2
D
2:1

Solution

## The correct option is A $$3:2$$Let there be $$n_1$$ boys and $$n_2$$ girls. and  $$\overline{X_1}$$ and $$\overline{X_2}$$ be the average scores of boys and girls respectively.Then $$\bar { { X }_{ 1 } } =71,\bar { { X }_{ 2 } } =63$$ and $$\overline{X} = 71.8$$$$\therefore \quad \overline{X} = \displaystyle\frac{n_1\overline{X_1} + n_2\overline{X_2}}{n_1+n_2}$$$$\Rightarrow \quad 71.8 = \displaystyle\frac{n_1\times71 + n_2\times73}{n_1+n_2}$$$$\Rightarrow \quad 71.8n_1 + 71.8n_2 = 71n_1 + 73n_2$$$$\Rightarrow \quad 0.8n_1 = 1.2n_2 \quad \Rightarrow 8n_1 = 12n_2$$$$\Rightarrow \quad \displaystyle\frac{n_1}{n_2} = \displaystyle\frac{12}{8} = \displaystyle\frac{3}{2}$$Mathematics

Suggest Corrections

0

Similar questions
View More

People also searched for
View More


# Difficult Probability Solved QuestionAptitude Discussion

 Q. Two squares are chosen at random on a chessboard. What is the probability that they have a side in common?
 ✔ A. 1/18 ✖ B. 64/4032 ✖ C. 63/64 ✖ D. 1/9

Solution:
Option(A) is correct

Sample space will be total ways of selecting 2 squares (order doesn't matter) out of available 64 squares.

So sample space$=^{64}C_2$

Now there are 7 unique adjacent square sets in each row and each column.

i.e. favourable cases will be 7×(8 rows + 8 columns) = 112.

Hence Required Probability

$=\left(\dfrac{\text{Favourable cases}}{\text{Sample Space}}\right)$

$=\dfrac{112}{^{64}C_2}$

$=\dfrac{1}{18}$

Also, there could be other solution too if we consider ALL possible squares and not only the smallest squares. (i.e. if we consider that total squares are more than 64).

In that case, let us first calculate the sample space i.e. total number of squares on a chess board.

1, 8x8 square
4, 7x7 squares
9, 6x6 squares
16, 5x5 squares
25, 4x4 squares
36, 3x3 squares
49, 2x2 squares
64, 1x1 squares

Therefore, there are actually:

$=64+49+36+25+16+9+4+1$

$=8^2+7^2+6^2+...1^2=204$

squares on a chessboard!

So sample space$=^{204}C_4$

Now if we assume that 2 squares can have a side common only if they are of the same dimensions:
Let the length of the smallest square be 1 unit.

So the possibility of selecting 2 squares with a common side (As calculated in the first method above):

having the side length greater than 4 unit length = 0
having the side length of 4 unit = 1×(2 rows + 2 columns) = 4
having the side length of 3 unit = 3×(6 rows + 6 columns) = 36
having the side length of 2 unit = 5×(7 rows + 7 columns) = 70
having the side length of 1 unit = 7×(8 rows + 8 columns) = 112

So total number of favourable cases:

$=4+36+70+112=222$

Hence Required Probability

$=\left(\dfrac{\text{Favourable cases}}{\text{Sample Space}}\right)$

$=\dfrac{222}{^{204}C_2}$

Edit: For an alternative solution, check comment from Bhavya Shah.

Edit 2: For yet another alternative solution, check comment by RandomMathMan.

## (12) Comment(s)

Gaurav Karnani
()

Total number of Cases for a chessboard$7*(7*2+1)$ that equals $112$

P= $112/64C2$

Gaurav Karnani
()

Forgot to add seven to the number of cases for the last row. please amend it accordingly.

RandomMathMan
()

There's a much easier way to do this. On a chessboard, there are 36 squares that border 4 other squares, 24 squares that border 3 other squares, and 4 squares that border only 2 other squares.

$\dfrac{36}{64} \times \dfrac{4}{63} + \dfrac{24}{64} \times \dfrac{3}{63} + \dfrac{4}{64} \times \dfrac{2}{63} = \dfrac{1}{18}$

Siddharth
()

Exactly how I too solved this. Much simpler than the other alternatives given here.

Bhavya Shah
()

For each row, there are 7 ways to select adjacent squares and there are 8 such rows.

Similarly for columns, 7 squares could be selected from one column and there are 8 such columns.

So no. of ways of selecting adjacent squares $= 7*8*2$

Total number of ways of selecting squares of chessboard $= ^{64}C_2$

Probability $=\dfrac{7*8*2}{^{64}C_2}=\dfrac{1}{18}$

Poonam Pipaliya
()

sir,,how 7 squares in each row and column remain..??..!! and how it can be favorable case..??..!!

Anuj
()

Lets talk of the rows first. Once you pick up a square, you leave that row, so you have 7 remaining rows.

Similar argument is valid for columns.

Rishi
()

I guess the alternative method given in the solution is easier to understand, why don't you have a look at that.

Arshal
()

Guys, I think I'm right but, as I have been hitting my head on the wall of probability for last 7 hours I'm kinda in concussion...

So is choosing 2 consecutive elements out of 8 equal to choosing 1 element out of 7

()

$C(4,1)*2+C(24,1)*3+C(36,1)/C(64,2)$

There are 4 corner squares for which there are only 2 choices for selecting next square....
Similarly, 24 squares for which there are 3 choices and remaining 36 have 4 choices

So, the answer should be 1/9

Correct me if i am wrong!!!

()

As I counted every square non repeated

So, total no of unique square=

$\dfrac{((C(4,1)*2+C(24,1)*3+C(36,1)*4)/2)}{C(64,2)}$

$=\dfrac{1}{18}$


# Thread: calculating compound interest with non-linear interest growth

1. ## calculating compound interest with non-linear interest growth

Hello, I would really appreciate some help with the following problem.

I'd like to derive a formula to determine the value of an investment where the annual yield increases by a fixed percentage each year. The interest paid is re-invested each year. The standard compounding formulas use a fixed yield so I've not been able to use them in this case.

As an example, suppose I have 1000 dollars in an investment with an initial starting yield (y) of 3% which grows (g) at 5% each year.

End of Year 1 : Yield was 3.00%, Investment value = 1000 + (1000 * 3%) = 1030.00
End of Year 2 : Yield was 3.15% (3% increased by 5%), Investment value = 1030 + (1030 x 3.15%) = 1062.45
End of Year 3 : Yield was 3.31% (3.15% increased by 5%), Investment value = 1062.45 + (1062.45 x 3.31%) = 1097.59

I believe the calculation is a series in the form

Value = P.(1+y0).(1+y1).(1+y2).(...)

where P = initial investment amount, r0 = yield in year 0, r1 = yield in year 1, r2 = yield in year 2 etc.
and where y0 = y.(1+g)^t etc.

If I write the above formula out substituting y0, y1, etc. I get

Value = P.(1+y(1+g)^t).(1+y(1+g)^(t-1)).(1+y(1+g)^(t-2)).(...) where t ranges from 0 to the target year n.

Is it possible to convert this expression into a general formula in the form of Value = f(t) where t = time in years, e.g. some kind of exponential function and how might I go about doing that?

Thank you for any help or insight that you can provide me!

~ Trevor

2. ## Re: calculating compound interest with non-linear interest growth

One way of doing this would be to find a single yield that results in a same future value using the series of yields

$FV = R (1+i)^n$

$ln(1+i_{s})=\frac{1}{n}\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)$

$i=e^{ln(1+i_s)}-1$

Code:
R=1000
r=3%
g=5%
n=3
$ln(1+i_{s})=0.031037549112832$

$i=e^{0.031037549112832}-1$

$i=3.15242359792658\%$

$FV = 1000 (1+3.15242359792658\%)^3$

$FV = 1000 (1.0315242359792658)^3$

$FV = 1000 (1.097585368)$

$FV = 1,097.59$

3. ## Re: calculating compound interest with non-linear interest growth

Hi GhostAccount,

Thank you for the fast response, that was very helpful! I've checked the results manually using Excel and your method generates all FV values that I expect from n=1 to n=10. There is one point that I don't fully understand however.

I understand that your answer is solving the formula below for i :

$R (1+i)^n = R\sum_{k\ =\ 0}^{n-1} (1+r(1+g)^k)$

This tells me that I should be able to calculate any given FV for a value of n by manually summing the rhs. However I don't get the expected answer when I tried to do this in Excel:

n | Expected vs Excel
1 | 1030.00 vs 1030.00 ( = 1000 + 30 )
2 | 1062.45 vs 1061.50 ( = 1000 + 30 + 31.5 )
3 | 1097.59 vs 1094.58 ( = 1000 + 30 + 31.5 + 33.075 )
4 | 1135.70 vs 1129.30 ( = 1000 + 30 + 31.5 + 33.075 + 34.7288 )

Did I interpret the FV summation formula correctly and just get Excel wrong?

I didn't realize that the compound growth + compound interest could be expressed as a summation like that - I thought this was only calculating the amount of interest that $1000 would produce in n years with a compounded rate and not compounding the principal amount as well. I was thinking that $FV=R(1+r(1+g)^0)(1+r(1+g)^1)(1+r(1+g)^2)$ etc A summation formula is what I really want to reach as I can then try to substitute a general identity for it and derive a formula in terms of r,g and n that I can use to calculate each expected value for a given t, for example using $\sum_{i\ =\ m}^{n-1} a^i = \frac{a^m - a^n}{1-a}$ Thanks again for your help! 4. ## Re: calculating compound interest with non-linear interest growth Originally Posted by tw35758 Hi GhostAccount, Thank you for the fast response, that was very helpful! I've checked the results manually using Excel and your method generates all FV values that I expect from n=1 to n=10. There is one point that I don't fully understand however. I understand that your answer is solving the formula below for i : $R (1+i)^n = R\sum_{k\ =\ 0}^{n-1} (1+r(1+g)^k)$ This tells me that I should be able to calculate any given FV for a value of n by manually summing the rhs. However I don't get the expected answer when I tried to do this in Excel: n | Expected vs Excel 1 | 1030.00 vs 1030.00 ( = 1000 + 30 ) 2 | 1062.45 vs 1061.50 ( = 1000 + 30 + 31.5 ) 3 | 1097.59 vs 1094.58 ( = 1000 + 30 + 31.5 + 33.075 ) 4 | 1135.70 vs 1129.30 ( = 1000 + 30 + 31.5 + 33.075 + 34.7288 ) Did I interpret the FV summation formula correctly and just get Excel wrong? Thanks again for your help! The formula I presented in two parts at first finds the continuously compounded yearly rate (geometric average) that has to be converted to a yearly rate See the following for complete formula $R (1+i)^n = R(e^{\sum_{k\ =\ 0}^{n-1} (1+r(1+g)^k)})$ Edit: This formula should work as I am short on time now thus I didn't find time to confirm the results 5. ## Re: calculating compound interest with non-linear interest growth Was trying for geometric series...like the 1000 bucks creates a guaranteed 30 bucks annual deposit (the 3%), so we'd have FV of 1000 plus FV of 30 annuity....but finally realised that won't work... i = initial interest rate (.03) f = increase factor (1.05) n = number of years (make it 10) FV = (1 + i*f^0)(1 + i*f^1)(1 + i*f^2)...........(1 + i*f^(n-1)) = 1.4481148... That's for the proverbial one dollar, of course; so 1,448.11 if$1000.

Soooo....looks like I'm kinda repeating GA's "spectacular!" work.

Whadda hell is the matter with the dollar sign here??

6. ## Re: calculating compound interest with non-linear interest growth

Originally Posted by GhostAccount
The formula I presented in two parts at first finds the continuously compounded yearly rate (geometric average) that has to be converted to a yearly rate

See the following for complete formula

$R (1+i)^n = R(e^{\sum_{k\ =\ 0}^{n-1} (1+r(1+g)^k)})$

Edit: This formula should work as I am short on time now thus I didn't find time to confirm the results
Beautiful.
It should work but there's a slight typo on the exponent of e; $ln$ is missing.This could have been

$R (1+i)^n = R(e^{\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k})$

Must agree with Sir Wilmer; spectacular work indeed.
Was about to work on a derivation for this one when i saw it earlier but got too sober for a day and a half.
Drunk as I am right now, I am nevertheless definitely green with envy for this piece of beauty.
I'm almost certain I'm not the only one who'd like to see the analysis of this derivation Sir GhostAccount.
But as you said, this is one way of doing this. There might be another (or other's).
Might have to soak me brain with me favorite tequila brand for one such other future possibe derivation.

7. ## Re: calculating compound interest with non-linear interest growth

Thanks everyone, I've learned a lot so far!

I did some more research and realized that the correct way to represent the FV formula for the compounded growth of compounded interest is

$FV = R(\prod_{k\ =\ 0}^{n-1} (1+r(1+g)^k)})$

Since FV is essentially R(a)(b)(c)(...) we can convert this to a summation using logs.

Since

$e^{ln(x)} = x$

and

$ln(abc) = ln(a)+ln(b)+ln(c)$

so

$\boxed{FV= R(e^{\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})}$

$FV=R(1+i)^n$

so

$R(1+i)^n= R(e^{\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})$

eliminating R and taking the log of both sides we get

$nln(1+i) = ({\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})$

$ln(1+i) = \frac{1}{n}({\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})$

$1+i = e^{\frac{1}{n}({\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})}$

and now we're back to the answer GhostAccount initially provided as

$\boxed{i = e^{\frac{1}{n}({\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)})} - 1}$

One last question...is it possible to express

${\sum_{k\ =\ 0}^{n-1} ln(1+r(1+g)^k)}$

in a single function f(n) where it outputs the expected FV for n=0, n=1, n=2 etc?

I could do something similar for the normal compounded case, e.g.

$\sum{r(1+g)^k} = \frac{r((1+g)^{(k+1)}-1)}{g}$

but I'm completely lost on how to do a similar conversion for the FV formula or if it's even possible. I'm trying to create an Excel file comparing the result of various investments with different dividend yields and dividend growth rates, so having a single formula in a cell for each value of n would make creating that file much easier.

8. ## Re: calculating compound interest with non-linear interest growth

Whadda hell is the matter with the dollar sign here??
Dollar sign invokes the LaTeX editor. Confused me when I started posting here. See Dollar Signs

9. ## Re: calculating compound interest with non-linear interest growth

I realized I should make my question more specific...
Suppose I have two different dividend paying stocks - one has an initial yield r1 with a growth rate of r1 and the second has an initial yield of r2 with a growth rate of r2.
I'd like to be able to analyze / compare the two performance stocks when I re-invest the dividend payments and answer questions such as:

Q1: With a constant stock price, what is the total return of principal + dividends after time t?

Q2: How many years will either stock take to reach a certain dividend payout?

I was able to do this for the non-compounding case and I posted an article on my blog explaining the formulae. The formulae and explanations above are really helpful but I'm stuck in transforming the summation formula above to a more usable form. I want to avoid continous compounding but keep to annually compounded calculations.

10. ## Re: calculating compound interest with non-linear interest growth

Originally Posted by tw35758
but I'm completely lost on how to do a similar conversion for the FV formula or if it's even possible. I'm trying to create an Excel file comparing the result of various investments with different dividend yields and dividend growth rates, so having a single formula in a cell for each value of n would make creating that file much easier.

For now, see the following code in Excel to use the tadFVSchedule function that answers the original question

Code:
Public Function tadFVSchedule(ByVal pv As Double, ByVal r As Double, ByVal g As Double, ByVal n As Double, Optional ByRef c As Double = 1, Optional ByRef p As Double = 1, Optional ByRef d As Double = 1) As Double
Dim fraction As Double
Dim sum As Double
Dim k As Integer

sum = 0#

fraction = n - Int(n)

For k = 0 To Int(n - 1)
sum = sum + p * d * Log(1 + r * (1 + g) ^ k)
Next k

If (fraction <> 0) Then
k = Int(n + 0.5)
sum = sum + ((fraction - 1) * p + p * d) * Log(1 + r * (1 + g) ^ k)
End If

End Function

 pv r g n c p d fv 1000 3% 5% 3 1 1 1 1097.59 1000 3% 5% 10 1 1 1 1448.11 1000 3% 5% 3.5 1 1 1 1117.42 1000 3% 5% 10.5 1 1 1 1484.80 1000 3% 5% 3 =1/12 1 1 1097.59 1000 3% 5% 10 =1/12 1 1 1448.11 1000 3% 5% 3 1 =1/12 1 1007.79 1000 3% 5% 10 1 =1/12 1 1031.34 1000 3% 5% 3 =1/12 =1/12 1 1007.79 1000 3% 5% 10 =1/12 =1/12 1 1031.34 1000 3% 5% 3 1 1 =1/2 1047.66 1000 3% 5% 10 1 1 =1/2 1203.38 1000 3% 5% 3.5 1 1 =1/2 1047.66 1000 3% 5% 10.5 1 1 =1/2 1203.38 1000 3% 5% 3 =1/12 1 =1/2 1047.66 1000 3% 5% 10 =1/12 1 =1/2 1203.38 1000 3% 5% 3 1 =1/12 =1/2 1003.89 1000 3% 5% 10 1 =1/12 =1/2 1015.55 1000 3% 5% 3 =1/12 =1/12 =1/2 1003.89 1000 3% 5% 10 =1/12 =1/12 =1/2 1015.55

11. ## Re: calculating compound interest with non-linear interest growth

Thank you GhostAccount! That's great - I can use that macro.

I made a typo in my question - I meant:

Suppose I have two different dividend paying stocks - one has an initial yield r1 with a growth rate of g1 and the second has an initial yield of r2 with a growth rate of g2. The stocks pay dividends annually and the dividends are re-invested to purchase more of the stock at the end of each year.

Q1: With a constant stock price, what is the total return of principal + dividends after time t?

Q2: How many years will either stock take to reach a certain dividend payout?

Thanks again for your help and patience in answering - I've learned a lot so far!

12. ## Re: calculating compound interest with non-linear interest growth

I must be missing something...but, if you're able to use a looper, then isn't that enough?

I love UBasic ([]= notes):

i = .03 : f = 1.05 : b = 1000 : p = 1000 : k = 30 [f = increase factor, k = years]
FOR n = 1 TO k
e = b * i [e = year's earnings]
b = b + e
y = (b / p)^(1 / n) [y = yield so far]
PRINT n, e, b, i * 100, y * 100
i = i * f [next year's rate]
NEXT n

OUTPUT:
Code:
YEAR  INTEREST  BALANCE  YEAR'S RATE  YIELD-SO-FAR
0             1000.00
1      30.00  1030.00    3.0000        3.0000
2      32.45  1062.45    3.1500        3.0750
3      35.14  1097.59    3.3075        3.1524
...
10      64.40  1448.11    4.6540        3.7720
...
20     185.22  2628.54    7.5808        4.9507
...
30     749.55  6819.59   12.3484**      6.6085
** .03 * 1.05^29 = 12.3484...

Isn't that all you need?

13. ## Re: calculating compound interest with non-linear interest growth

Hi Wilmer,

You're correct - I can certainly compute the answer using an excel macro. However I'm curious if there's a mathematical solution to this question.

The only mathematical solution I know of would be to approximate the answer using a Taylor series e.g.

$FV = Re^{\sum_{k\ =\ 0}^{n-1}ln({1+r(1+g)^k)}}$

$ln(1+x) \approx x - \frac{x^2}{2} + \frac{x^3}{3} - {...}$

taking the first 2 terms as an example then

$\sum_{k\ =\ 0}^{n-1}ln({1+x}) \approx \sum_{k\ =\ 0}^{n-1}(x - \frac{x^2}{2})$ where $x = r(1+g)^k$

$\sum_{k\ =\ 0}^{n-1}(x - \frac{x^2}{2}) = \sum_{k\ =\ 0}^{n-1}x - \sum_{k\ =\ 0}^{n-1}\frac{x^2}{2}$

and

$\sum_{k\ =\ 0}^{n-1}x = \sum_{k\ =\ 0}^{n-1}r(1+g)^k = \frac{r((1+g)^n-1)}{g}$

$\sum_{k\ =\ 0}^{n-1}\frac{x^2}{2} = \frac{1}{2}\sum_{k\ =\ 0}^{n-1}r^2(1+g)^{2k} = \frac{r^2((1+g)^{2n}-1)}{2g(g+2)}$

so I could say that

$FV \approx Re^{\frac{r((1+g)^n-1)}{g} - \frac{r^2((1+g)^{2n}-1)}{2g(g+2)}}$

and solve this equation for n with a given FV.

I'm wondering if there's a more elegant equivalent expression of this function out there that doesn't involve approximations.

14. ## Re: calculating compound interest with non-linear interest growth

Originally Posted by tw35758
I'm wondering if there's a more elegant equivalent expression of this function out there that doesn't involve approximations.
After 5 beer bottles and 2 shots of tequila, I've been wondering about that too. You could always repost your questions on an actuarial forum. You might get lucky there. Having seen your analyses, I'm somewhat inclined to believe that you might have some background in actuarial mathematics yourself. If you're lucky, Sir TKHunny, the resident actuary here might get interested in your situation and come out of retirement from this often thankless business of math knight-errantry.

15. ## Re: calculating compound interest with non-linear interest growth

Originally Posted by jonah
After 5 beer bottles and 2 shots of tequila, I've been wondering about that too...
Hey, that was my breakfast 30 years ago, at end of my drinking...!!

Page 1 of 2 12 Last


# Math Help - Trig Integral

1. ## Trig Integral

$\int SEC^4{5x} = \int ({SEC^2{5x}})({SEC^2{5x}})$
Then
$\int (1+TAN^2{5x})(SEC^2{5x})$
Then I multiply out
$\int (SEC^2{5x})+(SEC^2{5x}TAN^2{5x})$

I would assume U sub at this point
$U= SEC dU= SECTAN$
But I am not sure, what or how I should bring along with the SEC

Any help?

thanks
Rich

2. Originally Posted by Spoolx
$\int SEC^4{5x} = \int ({SEC^2{5x}})({SEC^2{5x}})$
Then
$\int (1+TAN^2{5x})(SEC^2{5x})$
Then I multiply out
$\int (SEC^2{5x})(SEC^2{5x}TAN^2{5x})$
Fine so far, except you're missing a plus sign in the last line, which I assume is a typo. Now to substitute, which is really in order to work backwards through the chain rule.

Just in case a picture helps...

... where (key in first spoiler) ...

Spoiler:

... is the chain rule. Straight continuous lines differentiate downwards (integrate up) with respect to the main variable (in this case ), and the straight dashed line similarly but with respect to the dashed balloon expression (the inner function of the composite which is subject to the chain rule).

The general drift is...

Spoiler:
$\int 1 + u^2\ du = u + \frac{1}{3} u^3$

and so we can explicate F...

Hope this helps - if not, someone will probably show the sub with differentials. However, I say...
_________________________________________

Don't integrate - balloontegrate!

Balloon Calculus; standard integrals, derivatives and methods

Balloon Calculus Drawing with LaTeX and Asymptote!

3. I edited the original to include the + sign, thanks for catching my mistake

4. $\displaystyle\int{sec^4(5x)}dx=\int{sec^2(5x)sec^2 (5x)}dx=\int{\left[1+tan^2(5x)\right]sec^2(5x)}dx$

Substitute

$u=tan(5x)$

$\displaystyle\Rightarrow\frac{du}{dx}=5sec^2(5x)$

by the chain rule

$\displaystyle\Rightarrow\ sec^2(5x)dx=\frac{du}{5}$

The integral becomes

$\displaystyle\frac{1}{5}\int{\left[1+u^2\right]du$

5. Hello, Rich!

A rehash of Archie's solution . . .

$\displaystyle \int \sec^4\!5x\,dx \:= \:\int (\sec^2\!5x)(\sec^2\!5x)\,dx$

Then: . $\displaystyle \int (1+\tan^2\!5x)(\sec^2\!5x)\,dx$

Then I multiply out: . $\displaystyle \int (\sec^2\!5x +\sec^2\!5x\tan^2\!5x)\,dx$

$\displaystyle \text{You have: }\;\int\sec^2\!5x\,dx + \int(\tan 5x)^2(\sec^2\!5x\,dx)$

For the first integral, use: . $\displaystyle \int \sec\theta\,d\theta \:=\:\tan\theta + C$

For the second integral, let: . $u \,=\, \tan x \quad\Rightarrow\quad du \,=\,\sec^2\!x\,dx$

. . and you have: . $\displaystyle \int u^2\,du \:=\:\tfrac{1}{3}u^3 + C$

Got it?

6. Originally Posted by Soroban
Hello, Rich!

A rehash of Archie's solution . . .

$\displaystyle \text{You have: }\;\int\sec^2\!5x\,dx + \int(\tan 5x)^2(\sec^2\!5x\,dx)$

For the first integral, use: . $\displaystyle \int \sec\theta\,d\theta \:=\:\tan\theta + C$

For the second integral, let: . $u \,=\, \tan x \quad\Rightarrow\quad du \,=\,\sec^2\!x\,dx$

. . and you have: . $\displaystyle \int u^2\,du \:=\:\tfrac{1}{3}u^3 + C$

Got it?
So to refresh my algebra

$tan^2{5x} = (tan5x)^2$ ?

Also above, is your U supposed to be?
$u= tan5x$ ?
if not where does the 5 go?

7. Originally Posted by Spoolx
$tan^2{5x} = (tan5x)^2$ ? Mr F says: Yes.
$u= tan5x$ ? Mr F says: Yes. Soroban made a simple typo. You ought to be able to make the appropriate corrections in the calculations.


# How to calculate an integral using Cauchy's theorem?

The question is calculate the value of the integral, $$\int_{-\infty}^{\infty}\frac{dx}{1+x^4}$$ These are the steps that I followed,

Let $x$ be a complex number. So, the poles of the function $f(x)=\frac{1}{1+x^4}$ occur when $x$ is equal to the roots of the equation $1+x^4=0$, i.e $x=e^{i\pi/4},e^{3i\pi/4},e^{5i\pi/4},e^{7i\pi/4}$. They are all poles of degree 4.

Now Cauchy's theorem says that, $$\frac{1}{2\pi i}\int_C dx\ f(x)=\sum_i\text{Res}(f,x_i)$$ where $x_i$ are the poles of $f$ that lies within $C$. I am pretty sure that my poles lie within $-\infty$ and $\infty$. So, I calculated the residues of $\frac{1}{1+x^4}$ at $x=e^{i\pi/4},e^{3i\pi/4},e^{5i\pi/4},e^{7i\pi/4}$ and they are equal to $-\frac14e^{i\pi/4},-\frac14e^{3i\pi/4},-\frac14e^{5i\pi/4},-\frac14e^{7i\pi/4}$. Wolfram Alpha confirms my calculations.

The sum of residues is $$\sum_i\text{Res}=-\frac14e^{i\pi/4}-\frac14e^{3i\pi/4}-\frac14e^{5i\pi/4}-\frac14e^{7i\pi/4}=0$$ and therefore the integral, $$\int_{-\infty}^{\infty}\frac{dx}{1+x^4}$$ must be equal to zero. However, Wolfram alpha says it is not zero but equal to$\frac{\pi}{\sqrt 2}$. Where am I making a mistake?

In applying the residue theorem, we analyze the integral $I$ given by

\begin{align} I&=\oint_C \frac{1}{1+z^4}\,dz\\\\ &=\int_{-R}^R \frac{1}{1+x^4}\,dx+\int_0^\pi \frac{1}{1+(Re^{i\phi})^4}\,iRe^{i\phi}\,d\phi\\\\ &=2\pi i \,\text{Res}\left(\frac{1}{1+z^4}, z=e^{i\pi/4},e^{i3\pi/4}\right) \end{align}

where $R>1$ is assumed.

Note that the only residues implicated in the residue theorem are those enclosed by $C$. Here, $C$ is comprised of (i) the line segment from $-R$ to $R$ and (ii) the semicircular arc centered at the origin with radius $R$ and residing in the upper-half plane. Hence, the only resides are the ones at $z=e^{i\pi/4}$ and $z=e^{i3\pi/4}$.

Taking $R\to \infty$, the integration over the semi-circular contour vanishes and we find that

\begin{align} \int_{-\infty}^\infty \frac{1}{1+x^4}\,dx&=2\pi i \,\text{Res}\left(\frac{1}{1+z^4}, z=e^{i\pi/4},e^{i3\pi/4}\right)\\\\ &=2\pi i \left(-\frac{e^{i\pi/4}}{4}-\frac{e^{i3\pi/4}}{4}\right)\\\\ & =\frac{\pi}{\sqrt 2} \end{align}

• Like the other answer, why did you only used the upper half semicircle? Why did you not ignore the first term in the second step and evaluate over the whole circle like, $$\int_0^{2\pi} d\phi\ \frac{iRe^{i\phi}}{1+(Re^{i\phi})^4}$$ – sigsegv Apr 23 '17 at 17:13
• @Ayatana We want to evaluate the integral $\int_{-\infty}^\infty \frac{1}{1+x^4}\,dx$. How does integrating over the circle do that? Well, it doesn't; and that integral is $0$. So instead, we evaluate a contour integral, the one described herein, that has the real line integral as part of the contour. We could have taken the semi-circle in the lower-half plane, but since the orientation would be clockwise, we need a $-2\pi i$ factor o multiply the sum of the residues in the lower-half plane. The result will be the same, of course. ;-)) – Mark Viola Apr 23 '17 at 17:15
• Is it necessary for the contour to be semicircle? Will a contour made of a straight line along real axis and an arbitrary line joing the ends work? – sigsegv Apr 24 '17 at 4:30
• @Ayatana The reside theorem applies to any closed rectifiable curve. But we are free to choose the closed contour. And by choosing a semicircle the radius of which approaches infinity, we can show quite easily that its contribution approaches $0$ as $R\to \infty$. – Mark Viola Apr 24 '17 at 4:34

You shouldn't need Wolfram Alpha to tell you the answer isn't zero. The integrand is positive, so the integral is positive, and certainly nonzero.

You have added up the residues at all the poles. However using the usual semicircle method gives you that the integral is $2\pi i$ times the sum of the residues of the poles in the upper half-plane.

• Why should I not add the residues of the poles below the lower half? What semi circle are you talking about? The limits of the integration are from $-\infty$ to $\infty$ for both real and imaginary parts of $x$. – sigsegv Apr 23 '17 at 16:29


# Version of Banach-Steinhaus theorem

I am wondering about the following version of the Banach-Steinhaus theorem.

Let $A$ be a closed convex subset contained in the unit ball of a Banach space $X$ and consider bounded operators $T_n \in \mathcal L(X).$

Assume we know that for every $x \in A$ the sequence $\left\lVert T_n x \right\rVert$ is bounded uniformly in $n.$

Does this imply that $$\sup_{x \in A} \left\lVert T_n x \right\rVert$$ is bounded uniformly in $n$?

If $A=X$ then the theorem is undoubtedly true by the folklore Banach-Steinhaus theorem but I was wondering whether this version holds as well?

• In the last paragraph, shouldn't it be "if A is the unit sphere then..." ? – user111 Jul 10 '18 at 7:45
• It is worth noting that the implication does not hold without the closeness assumption. If e.g. $A$ is the convex hull of the standard basis of $X:=\ell_2$ and $\phi_n:=\sum_{k=1}^n ke_k\in X$, then $(\phi_n,x)$ is point-wise but not uniformly bounded for $x\in A$ – Pietro Majer Jul 10 '18 at 15:16

The answer is yes, as a close inspection of the standard proof of the uniform boundedness principle/Banach-Steinhaus theorem shows. The standard proof (or at least the proof which I would consider to be the standard one) can e.g. be found on Wikipedia.

The details are a bit different here, so let me give them below. Throughout, let us replace the sequence $(T_n)$ with a general subset $\mathcal{T} \subseteq \mathcal{L}(X)$.

Proof. By Baire's Theorem we can find an integer $m$ such that the set \begin{align*} B := \{x \in A: \, \|Tx\| \le m \text{ for all } T \in \mathcal{T}\} \end{align*} has non-empty interior within $A$. Thus, we can find a point $x_0 \in B$ and a real number $\varepsilon \in (0,1]$ such that each $x \in A$ which has distance at most $\varepsilon$ to $x_0$ is contained in $B$.

Now, let $y \in A$. The vector $z := x_0 + \frac{\varepsilon}{2}(y-x_0)$ is contained in $A$ due to the convexity of $A$, and its distance to $x_0$ is at most $\varepsilon$ since both $y$ and $x_0$ have norm at most $1$. Thus, $\|Tz\| \le m$ for all $T \in \mathcal{T}$. Since \begin{align*} y = \frac{2}{\varepsilon}(z - x_0) + x_0, \end{align*} we conclude that \begin{align*} \|Ty\| \le \frac{4m}{\varepsilon} + m \end{align*} for all $T \in \mathcal{T}$. This bound does not depend on $y$.

Instead of inspecting the Banach-Steinhaus proof as in Jochen Glueck's answer one can apply Banach-Steinhaus to the Banach space $[A]$ (the linear hull of $A$) endowed with the Minkowski functional $\|x\|_A=\inf\{t>0: x\in tA\}$ (completeness of this norm follows from completeness of the Banach space $X$ and $A=\overline A$).

• A small detail: we need a balanced convex set to get a norm, so we should replace $A$ with $A':= \text{co}(A\cup (-A))=[-1,1]\cdot A$, and check that the hypotheses are still satisfied by $A'$ – Pietro Majer Jul 10 '18 at 7:22
• (the last equality is not true of course) – Pietro Majer Jul 10 '18 at 14:06

I like Jochen Wengenroth's approach, and I think there is a point that it is worth to clarify. If we want to make a norm out of $A$, we need it to be a balanced set, so we'd like to pass to the bounded absolutely convex set $\overline{\operatorname{co}}\left(A\cup(-A)\right)$ or to $A-A$. Any family of linear operators which is point-wise bounded on $A$ is clearly also point-wise bounded on $A-A$. However these sets are in general not closed, so some care is needed, because a bounded absolutely convex not closed set $B$ in general would not produce a Banach disk on its linear span, and in fact in general the statement itself does not hold on such $B$ (see the example in the initial comment).

A cheap solution to make the argument work smoothly is to use the notion of $\sigma$-convexity (see e.g. this MO thread) which also generalize slightly the statement); in particular, it covers both the case of a closed and an open bounded convex set $A$. Recall that for a subset $A$ of a Banach space $X$ the following easy facts hold:

• If $A$ is $\sigma$-convex, it is bounded;
• If $A$ is $\sigma$-convex, $A-A$ is $\sigma$-convex and symmetric (that is, $\sigma$-absolutely convex);
• If $A$ is $\sigma$-absolutely convex, it is a Banach disk, that is, its Minkowski functional is a Banach norm on the linear span of $A$.

As a conclusion, we can follow Jochen Wengenroth's reduction to the standard Banach-Steinhaus theorem. We thus have: Any family of linear operators on a Banach space, which is point-wise bounded on a $\sigma$-convex set $A$, is also uniformly bounded on $A$.


# Optimal strategies for this card game?

I recently had an interesting card game for an interview.

The interviewer and I have two cards each, one with a '5' painted on it and one with a '10'. We pick a card each and show it at the same time. If we picked the same card, I receive nothing, while if we picked different cards, the interviewer pays me the number he picked in dollars. What is the interviewer most likely to do if he intends to minimise my payoff, and what should my strategy be to counter this?

Consideration 1: I pick '5' all the time, but if the interviewer knows I do this he would pick '5' as well so that I receive nothing and my expected payoff would just be zero.

Consideration 2: I assign a probability $$p_1$$ of picking a '5' (and $$1-p_1$$ of picking a '10'), and the interviewer assigns a probability $$p_2$$ of picking a '5' (and $$1-p_2$$ of picking a '10'). My payoff would then be

$$\mathbb{E}=10p_1(1-p_2)+5(1-p_1)p_2.$$

I was thinking of differentiation somehow, but $$p_1$$ aims to maximise $$\mathbb{E}$$ while $$p_2$$ aims to minimise $$\mathbb{E}$$. Is this even the correct strategy for both of us?

Consideration 3: A friend suggested assigning probabilities of $$\frac13$$ and $$\frac23$$ to the cards respectively for a Nash equilibrium since it is a symmetric game. Where does this come from intuitively? Do these probabilities match with the above equation?

Any help/comments on the considerations is greatly appreciated, cheers!

The logic of equilibrium here is: you should choose your strategy (i.e. determine your probabilities of picking $$5$$ and $$10$$) in such a way that, however your opponent chooses his strategy, your expected gain is not influenced.

Let's say you pick $$5$$ with probability $$p_1$$ and your opponent picks $$5$$ with probability $$p_2$$. Then, once you fixed your choice of $$p_1$$, the expectation $$\mathbb{E}$$ is a linear function in $$p_2$$: $$\mathbb{E} = 10p_1 + (5 - 15p_1)p_2.$$ In this situation, you see that choosing $$p_1 = 1/3$$ will guarantee an expectation of $$10/3$$, regardless of $$p_2$$.

If, let's say, you choose $$p_1 < 1/3$$. Then there is the "risk" that your opponent somehow knows your strategy (e.g. via statistics from multiple rounds of the game), and then chooses $$p_2 = 0$$. You then have $$\mathbb{E} = 10p_1 < 10/3$$.

If, in the other direction, you choose $$p_1 > 1/3$$. Then again assume your opponent gets to know your strategy, and then chooses $$p_2 = 1$$. You then have $$\mathbb{E} = 5 - 5p_1 < 10/3$$.

Therefore, you see that the "safest" way of playing is to choose $$p_1 = 1/3$$. It is in that sense that we say it's the "optimal strategy".

I hope this whole logic makes sense to you.

And as an exercise, you may try to solve the general situation: say the two cards are labelled A and B, and your gain has $$4$$ possibilities: $$a, b, c, d$$, which correspond to the $$4$$ combinations AA, AB, BA, BB of your cards. Then what is your optimal strategy?

• Hello, what are some general approaches to formulate equilibrium for a game? – user107224 Nov 13 '19 at 9:56
• For the general equilibrium, the wiki page en.m.wikipedia.org/wiki/Nash_equilibrium may be a good starting point. Also note that in general there could be more than one equilibria. – WhatsUp Nov 13 '19 at 10:38


# Axis/Angle from rotation matrix

With r=RotationMatrix[a,{x,y,z}] I can compute a 3D rotation matrix from axis/angle representation.

Given a 3D rotation matrix r how can I compute a, x, y, z?

Example:

r = {{0.966496, -0.214612, 0.14081}, {0.241415, 0.946393, -0.214612}, {-0.0872034, 0.241415, 0.966496}}

The result should be a=20. Degree and {x,y,z}={2, 1, 2}/3 (or equivalent).

Edit: I am fine with any answer that gives the same r when applied to RotationMatrix.

PS: Sorry if the tag is wrong, I did not found a matching tag...

-
I'm not sure this question is well posed in the sense that the answer is non-unique. A simple example would be r=RotationMatrix[1,{1,0,0}]=RotationMatrix[-1,{-1,0,0}] are the same. So which one are you intending to find? – bill s Aug 6 '13 at 13:08
The one where the angle is in [0,2 pi]. I think it is well posed as angle and axis are directly linked and you can provide a unique answer with a perfectly reasonable additional assumption. – Danvil Aug 6 '13 at 13:43
Actually there was an answer earlier which was just fine but it was deleted? – Danvil Aug 6 '13 at 13:46
@Danvil it was deleted because it was incorrect, so he deleted it. It was incorrect, as it worked only for the example you provided and could not be generalized. – rcollyer Aug 6 '13 at 14:42
@Danvil I'm thankful I noticed the wink. It would have been a bad day, otherwise. :P – rcollyer Aug 6 '13 at 16:38

There is no need to use Eigensystem or Eigenvectors to find the axis of a rotation matrix. Instead, you can read the axis vector components off directly from the skew-symmetric matrix

$$a \equiv R^T-R$$

In three dimensions (which is assumed in the question), applying this matrix to a vector is equivalent to applying a cross product with a vector made up of the three independent components of $a$:

{1, -1, 1}Extract[a, {{3, 2}, {3, 1}, {2, 1}}]

This one-line method of finding the axis is applied in the following function. To get the angle of rotation, I construct two vectors ovec, nvec perpendicular to the axis and to each other, to find the cosine and sine of the angle using the Dot product (could equally have used Projection). To get a first vector ovec that is not parallel to the axis, I permute the components of the axis vector using the fact that

Solve[{x, -y, z} == {y, z, x}, {x, y, z}]
(* ==> {{x -> 0, y -> 0, z -> 0}} *)

which means the above permutation with sign change of a nonzero axis vector is always different from the axis. This is sufficient to use Orthogonalize and Cross to get the desired orthogonal vectors.

axisAngle[m_] := Module[
{axis, ovec, nvec
},
{axis, ovec} =
Orthogonalize[{{1, -1, 1} #, Permute[#, Cycles[{{1, 3, 2}}]]}] &@
Extract[m - Transpose[m], {{3, 2}, {3, 1}, {2, 1}}];
(* nvec is orthogonal to axis and ovec: *)
nvec = Cross[axis, ovec];
{axis,
Arg[Complex @@ (((m.ovec).# &) /@ {ovec, nvec})]}
]

The angle is calculated with Arg instead of ArcTan[x, y] here because the latter throws an error for x = y = 0.

Here I test the results of the function for 100 random rotation matrices:

testRotation[] := Module[
{m, a, axis, ovec, nvec,
v = Normalize[RandomReal[{0, 1}, {3}]],
α = RandomReal[{-Pi, Pi}], angle
},
m = RotationMatrix[α, v];
{axis, angle} = axisAngle[m];
Chop[
angle Dot[v, axis] - α
] === 0
]

And @@ Table[testRotation[], {100}]

(* ==> True *)

In the test, I have to account for the fact that if the function axisAngle defined the axis vector with the opposite sign as the random test vector, I have to reverse the sign of the rotation angle. This is what the factor Dot[v, axis] does.

Explanation of how the axis results from a skew-symmetric matrix

If $\vec{v}$ is the axis of rotation matrix $R$, then we have both $R\vec{v} = \vec{v}$ and $R^T\vec{v} = \vec{v}$ because $R^T$ is just the inverse rotation. Therefore, with $a \equiv R^T-R$ as above, we get

$$a \vec{v} = \vec{0}$$

Now the skew-symmetric property $a^T = -a$, which can be seen from its definition, means there are exactly three independent matrix element in $a$. They can be arranged in the form of a 3D vector $\vec{w}$ which must have the property $a \vec{w} = 0$. This vector is obtained in the Extract line above. In fact, $a \vec{x} = \vec{w}\times \vec{x}$ for all $\vec{x}$, and hence if $a \vec{x} = 0$ then $\vec{x}\parallel\vec{w}$. Therefore, the vector $\vec{v}$ is also parallel to $\vec{w}$, and the latter is a valid representation of the rotation axis.

Edit 2: speed considerations

Since the algorithm above involves only elementary operations that can be compiled, it makes sense that a practical application of this approach would use Compile. Then the function could be defined as follows (keeping the return values arranged as above):

Clear[axisAngle1, axisAngle]

axisAngle1 =
Compile[{{m, _Real, 2}},
Module[{axis, ovec, nvec, tvec, w, w1},
tvec = {m[[3, 2]] - m[[2, 3]], m[[3, 1]] - m[[1, 3]],
m[[2, 1]] - m[[1, 2]]};
If[tvec == {0., 0., 0.},
{#/Sqrt[#.#] &[#[[Last@Ordering[N[Abs[#]]]]] &[
1/2 (m + {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})]],
If[Sum[m[[i, i]], {i, 3}] == 3, 0, Pi] {1, 1, 1}},
axis = {1, -1, 1} tvec;
axis = axis/Sqrt[axis.axis];
w = {tvec[[2]], tvec[[3]], tvec[[1]]};
ovec = w - axis Dot[w, axis];
nvec = Cross[axis, ovec];
w1 = m.ovec;
{axis, {1, 1, 1} ArcTan[w1.ovec, w1.nvec]}
]
]
];

axisAngle[m_] := {#1, Last[#2]} & @@ axisAngle1[m]

The results are the same as for the previous definition of axisAngle, but I now get a much faster execution as can be seen in this test:

tab = RotationMatrix @@ # & /@
Table[{RandomReal[{-Pi, Pi}],
Normalize[RandomReal[{0, 1}, {3}]]}, {100}];
timeAvg =
Function[func,
Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0,
15}], HoldFirst];

timeAvg[axisAngle /@ tab]

(* ==> 0.000801259 *)

This is more than an order of magnitude faster than the un-compiled version. I removed Orthogonalize from the code because I didn't find it in the list of compilable functions.

Note that Eigensystem is not in that list, either.

Edit 3

The first version of axisAngle demonstrated the basic math, but the compiled version axisAngle1 (together with the re-defined axisAngle as a wrapper) is faster. One thing that was missing was the correct treatment of the edge case where the rotation is by exactly $\pi$ in angle.

I added that fix only to the compiled version (axisAngle1) because I think that's the more practical version anyway. The trivial case of zero rotation angle was already included in the earlier version.

To explain the added code, first note that for angle $\pi$ you can't read off the axis from $R^T - R$ because the resulting matrix vanishes. To get around this singular case, we can use the geometric fact that a rotation by $\pi$ is equivalent to an inversion in the plane perpendicular to the rotation axis given by the unit vector $\vec{n}$. Therefore, if we form the sum of a vector $\vec{v}$ and its $\pi$-rotated counterpart, the components transverse to the rotation axis cancel and the result is always parallel to the axis. In matrix form,

$$(R+1)\vec{v} = 2\vec{n}(\vec{n}\cdot\vec{v}) = 2\left(\vec{n}\vec{n}^T\right)\vec{v}$$

Since this holds for all vectors, it is a matrix identity. The right-hand side contains a matrix $\vec{n}\vec{n}^T$ which must have at least one row that's nonzero. This row is proportional to $\vec{n}^T$, so you can read of the axis vector directly from $(R+1)$, again without any eigenvalue computations.

-
Beautiful! (+1) My hamfisted approach to this has been an opportunity to learn. – ubpdqn Aug 6 '13 at 22:28
Note (as you have outlined) you could also obtain the axis from NullSpace[m-Transpose[m]] – ubpdqn Aug 7 '13 at 4:17
@ubpdqn True, but that takes more computation... as I said, you can read the axis off literally from the matrix a itself. – Jens Aug 7 '13 at 5:15
Actually for cases $\theta=0$ and $\theta=\pi$ the $R-R^T$ is a zero matrix and the axis is thus maps to {{0,0,0},0) in your code. The test set always skirts these "points". The eigensystem allows extraction of the eigenvector associated with eigenvalue 1). I agree with lack of simplicity or elegance using the eigensystem. I found it a lazy way to deal with these anomalous cases: integer*$\pi$. I think my code works now. Passes your test (up orientation of axis issue) and the "extreme" cases. – ubpdqn Aug 7 '13 at 5:29
Maybe the easiest way to deal with the isotropic cases ($\pm$IdentityMatrix[3]) would be to append the rule axis /. {0, 0, 0} -> {0, 0, 1} to the result. The choice of rotation axis is arbitrary in that case and I'd fix it to be the z axis. – Jens Aug 7 '13 at 5:41

Since this question still seems to be alive after some time, I'm giving a solution from Presentations, which I sell. It has a routine RotationAngleAndAxis and here it is used on the example.

<< Presentations

r = {{0.966496, -0.214612, 0.14081}, {0.241415,
0.946393, -0.214612}, {-0.0872034, 0.241415, 0.966496}};
RotationAngleAndAxis[r]

(* {0.349066, {0.666667, 0.333333, 0.666667}} *)

Here is a short example of its use that checks the result. The axis vector is normalized. Notice that reversing the angle of rotation and the axis gives an equivalent rotation.

rotations =
Table[{RandomReal[{-\[Pi], \[Pi]}],
Normalize[Array[RandomReal[{-1, 1}] &, {3}]]}, {2}]

(* {{2.90598, {-0.596373, -0.74938,
0.287697}}, {-2.44158, {0.331763, -0.943343, 0.00605196}}} *)

rotationMatrices = RotationMatrix @@@ rotations;
anglesAndAxes = RotationAngleAndAxis /@ rotationMatrices

(* {{2.90598, {-0.596373, -0.74938, 0.287697}}, {2.44158, {-0.331763,
0.943343, -0.00605196}}} *)

resultingMatrices = RotationMatrix @@@ anglesAndAxes;

rotationMatrices - resultingMatrices // Chop
Total[Flatten[%]]

(* {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}} *)
(* 0 *)

The following is a test on 1000 initial rotation specifications. Notice also that I am comparing the resulting rotation matrices by a slightly risky procedure.

rotations =
Table[{RandomReal[{-\[Pi], \[Pi]}],
Normalize[Array[RandomReal[{-1, 1}] &, {3}]]}, {1000}];
rotationMatrices = RotationMatrix @@@ rotations;
Timing[anglesAndAxes = RotationAngleAndAxis /@ rotationMatrices;]
resultingMatrices = RotationMatrix @@@ anglesAndAxes;
Total[Flatten[rotationMatrices - resultingMatrices // Chop]]

(* {0.873606, Null} *)
(* 0 *)

Presentations also has an EulerAngles routine that will return the two sets of Euler angles for any of the possible sequence of rotations specified by strings. For example, here are the two sets of Euler angles of the example for two different rotation sequences.

EulerAngles[r, "ZXZ"]
EulerAngles[r, "XYZ"]

(* {{-0.346633, 0.259587, 0.580661}, {2.79496, -0.259587, -2.56093}} *)
(* {{0.244775, 0.0873143, 0.244775}, {-2.89682, 3.05428, -2.89682}} *)

-

The axis of rotation $n$ can be obtained as the eigenvector corresponding to the eigenvalue $1$.

r = {{0.966496, -0.214612, 0.14081}, {0.241415,
0.946393, -0.214612}, {-0.0872034, 0.241415, 0.966496}};

es = Eigensystem[r];
pos = Nearest[Thread[es[[1]] -> Range[3]], 1][[1]];
n = es[[2, pos]]
(*  {0.666667, 0.333333, 0.666666} *)

The angle of rotation can be obtained directly from the trace of $r$.

ArcCos[(Tr[r] - 1)/2] 180/π
(* 20. *)

(This was my earlier and longer way to obtain the angle of rotation. As rcollyer pointed out this will have issues if $\pi k,k\in \mathcal{Z}$)

The rotation matrix in terms of the angle of rotation $\theta$ and axis of rotation $\{n1, n2, n3\}$.

R[t_, {n1_, n2_, n3_}] :=
With[{n = {{0, -n3, n2}, {n3, 0, -n1}, {-n2, n1, 0}}, id = IdentityMatrix[3]},
Inverse[id - t n].(id + t n)]

Here $t =\tan \left(\frac{\theta }{2}\right)$.

We get three solutions from the element in the first position.

eqns = Flatten[R[t, es[[2, pos]]] - r] // Simplify;
sols = NSolve[eqns[[1]] == 0, t]
(* {{t -> 3.88248*10^16}, {t -> 0.176327}, {t -> -0.176327}} *)

Choose the one which satisfies all the entries.

Chop[eqns /. sols, 10^-5];
Position[%, {0 ..}];
solst = Extract[sols, %]
(*  {{t -> 0.176327}}  *)

Obtain it in terms of degrees.

2 ArcTan[t] 180/π /. solst
(* {20.} *)

-
It doesn't seem to work with a rotation of $\pi$ about the {0,0,1}. – rcollyer Aug 6 '13 at 14:39
@rcollyer, that is to be expected because the rotation group $SO(3)$ cannot be covered by a single chart. Each chart will have its cases that are problematic. I used this parametrization and it worked for the OP's question. Still using this and going with sols = NSolve[eqns[[2]] == 0, t] we get $t=0$ for your case. The technique I used for the axis of rotation should be fine for all cases, however. – Suba Thomas Aug 6 '13 at 14:55
True, but I think it is amenable to decomposition into the generators of rotation, i.e. the rotations should have eigenvalues of the form $\pmatrix{e^{i \theta} & 1 & e^{-i \theta}}$, or for real rotations $\pmatrix{\cos\theta + i \sin\theta & 1 & \cos\theta - i \sin\theta}$. Thus, the angle is easily recoverable. As to the direction, I have not yet looked through your code. – rcollyer Aug 6 '13 at 15:05
Thanks for pointing that out. With your approach the angle of rotation is ambiguous for the OP's question, i.e we don't know if it is +20 or -20, whereas with the parametrization I used only + 20 satisfied all conditions. Probably quaternions will give a unique correct solution, but I do not have the material before me now. – Suba Thomas Aug 6 '13 at 15:20
Well you can choose the angle to be between $0$ and $2 \pi$, to fix the branch. – rcollyer Aug 6 '13 at 15:25

My +1 to Jens.

For known Euler angles:

   {Φ, Θ, Ψ} = {0.1, 0.2, 0.3};
m = RotationMatrix[Φ, {0, 0, 1}].RotationMatrix[Θ, {1, 0,
0}].RotationMatrix[Ψ, {0, 0, 1}];
axisAngle[m_] :=Module[{axis, ovec, nvec}, {axis, ovec} =
Orthogonalize[{{1, -1, 1} #, Permute[#, Cycles[{{1, 3, 2}}]]}] &@
Extract[m - Transpose[m], {{3, 2}, {3, 1}, {2, 1}}];
nvec = Cross[axis, ovec];
{Arg[Complex @@ (((m.ovec).# &) /@ {ovec, nvec})], axis}];
N[axisAngle[m]]
(*  {0.446615, {0.448552, -0.0450053, 0.892623}}  *)
`
-