
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to Display Lower Triangular Matrix
In this article, we will learn how to write a swift program to display lower triangular matrix. Lower triangular matrix is a matrix in which all the elements above primary diagonal are zero. As shown in the below image:
$$\mathrm{\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\newline 3 & 5 & 0 & 0 & 0 & 0\newline 1 & 4 & 6 & 0 & 0 & 0\newline 1 & 2 & 2 & 2 & 0 & 0\newline 4 & 5 & 6 & 1 & 2 & 0\newline 1 & 1 & 1 & 4 & 5 & 3\newline \end{bmatrix}}$$
Lower triangular matrix
Here, (1, 5, 6, 2, 2, 3) is the primary diagonal so the elements above the primary diagonal are 0. Therefore, to create a lower triangular matrix we require a square matrix and then we convert the element to zero where the column is greater than the row.
Algorithm
Step 1 − Create a function.
Step 2 − In this function, first we check if the given matrix is a square or not
Step 3 − If the matrix is not a square matrix, then display a message.
Step 4 − If the matrix is a square matrix, then use a nested for loop to iterate through each row and column.
Step 5 − If the column position is greater than the row position, then replace the element with 0. Otherwise, print the elements.
Step 6 − Create a square matrix.
Step 7 − Call the function and pass the matrix into it as a parameter.
Step 8 − Print the output.
Example
Following Swift program to display lower triangular matrix.
import Foundation import Glibc // Size of the matrix var row = 5 var col = 5 // Function to print lower triangular matrix func printLowerTriangle(mxt:[[Int]]){ if (row != col){ print("Matrix is not a square matrix!\nso please enter a square matrix") } else { print("Lower Triangular Matrix:") for x in 0..<row{ for y in 0..<col{ if(x < y){ print("0", terminator:" ") } else { print(mxt[x][y], terminator: " ") } } print() } } } // Creating 5x5 matrix of integer type var matrix : [[Int]] = [[1, 3, 4, 5, 2], [2, 6, 7, 5, 7], [1, 5, 3, 1, 4], [2, 4, 3, 2, 4], [5, 2, 1, 3, 4]] print("Original Matrix:") for x in 0..<row{ for y in 0..<col{ print(matrix[x][y], terminator:" ") } print("\n") } // Calling the function printLowerTriangle(mxt:matrix)
Output
Original Matrix: 1 3 4 5 2 2 6 7 5 7 1 5 3 1 4 2 4 3 2 4 5 2 1 3 4 Lower Triangular Matrix: 1 0 0 0 0 2 6 0 0 0 1 5 3 0 0 2 4 3 2 0 5 2 1 3 4
Here in the above code, we have a 5x5 square matrix. Now we create a function to print a lower triangular matrix. In this function, we first check whether the given matrix is a square matrix or not. Here the matrix is a square matrix so now we find the upper triangular matrix by iterating through each row and column and if the column position is greater than the row then we replace the given element with zero. Otherwise, we print the elements.
Conclusion
So this is how we can display a lower triangular matrix. Or we can say by checking the position of the rows and columns( that is row<column) we convert the given square matrix into the lower triangular matrix.
- Related Articles
- Swift Program to Display Upper Triangular Matrix
- C# Program to Illustrate Lower Triangular Matrix
- Java Program to display upper triangular matrix
- Golang Program To Display Upper Triangular Matrix
- Program to check if matrix is lower triangular in C++
- JavaScript Program to check if the matrix is lower Triangular
- Program to print Lower triangular and Upper triangular matrix of an array in C
- How to replace upper triangular matrix with lower triangular matrix and vice versa in R?
- Print lower triangular matrix pattern from given array in C Program.
- Check if the matrix is lower triangular using Python
- How to create a heatmap for lower triangular matrix in R?
- C# program to Illustrate Upper Triangular Matrix
- JavaScript Program to Check if Matrix is Upper Triangular
- Swift Program to Display Fibonacci Series
- Swift program to Print Mirror Lower Star Triangle Pattern
