# Trending December 2023 # Swift Program To Check If A Given Square Matrix Is An Identity Matrix # Suggested January 2024 # Top 16 Popular

You are reading the article Swift Program To Check If A Given Square Matrix Is An Identity Matrix updated in December 2023 on the website Daihoichemgio.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Swift Program To Check If A Given Square Matrix Is An Identity Matrix

In this article, we will learn how to write a swift program to check if a given square matrix is the identity matrix. The identity matrix is an MxM square matrix in which the main diagonal consists of ones and other elements are all zero. For example −

\$mathrm{M:=:begin{bmatrix}1 & 0 & 0 & 0newline0 & 1 & 0 & 0 newline0 & 0 & 1 & 0 newline0 & 0 & 0 & 1end{bmatrix}}\$

Whereas a square matrix is a matrix in which the number of rows is equal to number of columns and it may or may not contain zeros and ones, means apart from 0s and 1s it contain other numbers. For example −

\$mathrm{M:=:begin{bmatrix}1 & 3 & 4 & 0newline6 & 4 & 0 & 6 newline3 & 6 & 1 & 0 newline4 & 4 & 0 & 1end{bmatrix}}\$

Algorithm

Step 1 − Create a function to check the given matrix is the identity matrix.

Step 2 − In this function, first we check the given matrix is the square matrix by checking the total number of rows and columns.

Step 3 − Using for loop, we check all the elements present in the main diagonal should be one.

Step 4 − Using nested for loop, we check all the elements other than diagonals elements are zero.

Step 5 − If all the conditions mentioned in steps 2, 3, and 4 are true, then this function will return true. Otherwise, return false.

Step 6 − Create three test matrices of integer type.

Step 7 − Call the function and pass the created matrices as a parameter in it.

Step 8 − Print the output.

Example

Following Swift program to check if a given square matrix is identity matrix.

import Foundation import Glibc { if mxt.count != mxt.count { return false } for x in 0..<mxt.count { if mxt[x][x] != 1 { return false } } for m in 0..<mxt.count { for n in 0..<mxt.count { if m != n && mxt[m][n] != 0 { return false } } } return true } var matrix1 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] print("Is matrix 1 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix1)) var matrix2 = [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1]] print("Is matrix 2 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix2)) var matrix3 = [[2, 1, 4], [2, 1, 1], [4, 5, 0], [3, 4, 1]] print("Is matrix 3 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix3)) Output Is matrix 1 is an identity matrix?: true Is matrix 2 is an identity matrix?: false Is matrix 3 is an identity matrix?: false

Here in the above code, we create a function to check whether the given matrices are the identity matrices or not. Therefore, for that, we check the given matrix for three different conditions −

Given matrix is a square matrix or not?

The main diagonal of the given matrix consists of ones (all the elements of the main diagonal) or not.

Elements other than the main diagonal should be zero or not.

If any of the above conditions return false, then the given matrix is not the identity matrix. Alternatively, if all the above three conditions return true, then the given matrix is an identity matrix.

Conclusion

So this is how we can if a given square matrix is an identity matrix or not. If you multiply two identity matrices with each other, then the resultant matrix is also the identity matrix.

You're reading Swift Program To Check If A Given Square Matrix Is An Identity Matrix

## Swift Program To Calculate The Sum Of Rows Of Matrix Elements

A matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc.

In this article, we will learn how to write a swift program to calculate the sum of rows of matrix elements. Here we are using the following methods to find the sum of the rows elements −

Using nested for-in loop

Using in-built function

Method 1: Using nested for-in loop

Here we use nested for-in loop to find the sum of rows of matrix elements.

Algorithm

Step 1 − Create a function.

Step 2 − Create a variable named sum to store the sum. The initial value of sum = 0.

Step 3 − Find the number of rows and columns.

Step 4 − Run nested for-in loop to iterate through each row and column.

Step 5 − In this nested loop, add elements row-wise and store the result into sum variable.

Step 6 − After each row reset the value of sum = 0 for the sum of next row elements.

Step 7 − Create a matrix and pass it in the function.

Step 8 − Print the output.

Example

Following Swift program to calculate the sum of rows of matrix elements.

import Foundation import Glibc func rowElementsSum(matrix: [[Int]]) { var sum = 0 let noRow = matrix.count let noColumn = matrix.count for R in 0..<noRow { for C in 0..<noColumn { sum += matrix[R][C] } print("Sum of row (R) = (sum)") sum = 0 } } let M = [[1, 2, 2], [1, 1, 6], [7, 8, 3], [2, 4, 2]] print("Matrix is:") for x in 0..<M.count { for y in 0..<M.count { print(M[x][y], terminator:" ") } print("n") } rowElementsSum(matrix: M) Output Matrix is: 1 2 2 1 1 6 7 8 3 2 4 2 Sum of row 0 = 5 Sum of row 1 = 8 Sum of row 2 = 18 Sum of row 3 = 8

Here in the above code, we have a 4×3 matrix and then pass it in the function named rowElementsSum() to find the sum of the row’s elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add it to the corresponding row sum. After finishing the nested for loop this function return the row sums that is row 0 = 5, row 1 = 8, row 2 = 18 and row 3 = 8(according to the input matrix).

Method 2: Using in-built function

To find the sum of the rows of the given matrix elements we uses reduce(_:_:) function. This function returns a result by combining the elements of the array or any sequence using the given closure.

Syntax func reduce(_initial: Value, _next: Value)

Here, initial represent the value to use as an initial accumulating value. It passes to the next for the first time the closure is executed. And next represent a closure that combines an accumulating value and an element of the array into a new accumulating value which if further used for next call of the next closure or return to the caller.

Algorithm

Step 1 − Create a matrix.

Step 2 − Print the matrix.

Step 3 − Create an 1-D array to store the sum of the rows.

Step 4 − Run a for loop to iterate through each row.

Step 5 − Find the sum of the rows elements using reduce() function.

let sum = row.reduce(0, +)

Step 6 − Now store the sum of each row in the array using append() function.

RowsSum.append(sum)

Step 7 − Print the resultant array.

Example

Following Swift program to calculate the sum of rows of matrix elements.

import Foundation import Glibc let matrix = [[1, 2, 3, 4, 6], [4, 5, 6, 1, 1], [7, 8, 9, 5, 5]] print("Matrix is:") for x in 0..<matrix.count { for y in 0..<matrix.count { print(matrix[x][y], terminator:" ") } print("n") } var RowsSum = [Int]() for row in matrix { let sum = row.reduce(0, +) RowsSum.append(sum) } print("So the sums of the individual rows are:", RowsSum) Output Matrix is: 1 2 3 4 6 4 5 6 1 1 7 8 9 5 5 So the sums of the individual rows are: [16, 17, 34]

Here in the above code, we have a 5×3 matrix. Then create an empty 1-D array to store the sum of each row. The using for loop we iterate through each row of the given matrix and adds the elements of the row using reduce() function. This sum is then store in the RowsSum array. So according to the given matrix the final result is [16, 17, 34]. Here 16 is the sum of row 0 elements(1+2+3+4+6), 17 is the sum of row 1 elements(4+5+6+1+1) and so on.

Conclusion

So this is how we can calculate the sum of rows of matrix elements. Here using the above methods we can calculate the sum of any type of matrix like 6×6, 9×9, 4×3, 6×7, etc.

## What Is A Skills Matrix?

A skills matrix is a short, descriptive but incredibly useful picture of a team’s capabilities.

Skills matrix are the connection between your organisation’s goals and the actions it needs to take to complete them.

Boards should pay extra attention to skills matrix because the level of scrutiny on their performance is generally increasing dramatically. So, they must ensure they have the right skills to function correctly.

A free skills matrix template

A skills matrix is a visual representation of all skills available on a given team. It might track a small group, a department, or even an entire organisation, depending on its size.

Usually:

It is designed in a cross-reference graph format, with names on one axis and skills across the other.

Skills are usually further defined using a proficiency level so that readers can see

how

skilled a person is in a particular area.

These scales can vary. A typical example is to use numbered scales (1-3 or 1-5), with the higher numbers representing more proficiency.

Ability6

Here is a free Ability6 skills matrix template – clearly laid out and well organised. Download this template.

Why is a skills matrix important?

You wouldn’t build a house without the right materials, you wouldn’t bake a cake without the right ingredients, and you shouldn’t start a project at work without the right skills on your team.

Skills matrixes help you determine this. They’re clear, concise, handy for comparisons, and have the basic info you need to be sure of your team’s capacity.

How important is a skills matrix to boards?

Extremely important. Boards are teams too, so tracking the skills of each member is not only worthwhile but essential to ensuring a board’s success.

Boards are, after all, the bodies that set company direction. Their skill sets must reflect the company’s mission. So it’s important to know what skills you have on your board and, crucially, what gaps there are to fill.

How do you make a skills matrix?

It’s a simple process:

Determine what skills you need to take your project forward.

For a board, this means careful analysis of where your company is going and the expertise you need to get it there.

Determine the proficiency of your current team in those skills.

Use a scoring system like the one described above.

If possible, assess the willingness of team members to use their skills.

This matters a lot on boards because directors will often have skills in many areas and may only want to use some of them. For example, a director who spent years in banking before moving to marketing might prefer to focus on the latter.

Lay your data out on the matrix. Use a standard digital tool like Excel or Google Sheets for simplicity.

Who evaluates skill proficiency?

This depends on the context, and there is no one right answer.

You can ask the subjects to self-evaluate and give their score, but critics of this method will say it introduces the potential for bias through over/underconfidence.

Line managers could do it, although this is usually not applicable to board members.

Subjects could undergo a skills assessment, allowing independent observers to score them.

The matrix could use formal qualifications as a benchmark. Increasingly, board members are turning to specialised qualifications to realise their full potential.

In summary

Skills matrices are graphical representations of the skills available to an organisation.

When done correctly, they will show a team’s capacity to complete a task and highlight areas where new talent may need to be brought on board.

Given this potential, they are vital for boards.

## Swift Program To Find The Given Number Is Strong Or Not

A strong number is a special number in which the sum of the factorial of its digit is equal to the number itself. For example −

Number = 345

345! = 3! + 4! + 5! = 6 + 24 + 120 = 150

Here 345 is not a strong number because the sum of the factorial of its digit is not equal to the number itself.

Number = 145

145! = 1! + 4! + 5! = 1 + 24 + 120 = 145

Here 145 is a strong number because the sum of the factorial of its digit is equal to the number itself.

In this article, we will learn how to write a swift program to find the given number is strong or not.

Algorithm

Step 1 − Create a function.

Step 2 − Create a copy of the original number and initialize sum = 0.

Step 3 − Run while to find the last digit of the given number.

Step 4 − Now find the factorial of all the individual digits of the given number.

Step 5 − Add the factorial of the digits and store the result in the sum variable.

Step 6 − Compare the sum and the original number. Return true if the sum and original number are equal. Otherwise, return false.

Step 7 − Create a test variable to store the number and pass it to the function.

Step 8 − Print the output.

Example

Following the Swift program to find whether the given number is strong or not.

import Foundation import Glibc var originalNum = num var sum = 0 let lDigit = originalNum % 10 var factorial = 1 for x in 1...lDigit { factorial *= x } sum += factorial originalNum /= 10 } return sum == num } let myInput = 345 if CheckStrongNumber(num: myInput) { print("YES! (myInput) is a strong number.") } else { print("NO! (myInput) is not a strong number.") } let myInput2 = 145 if CheckStrongNumber(num: myInput2) { print("YES! (myInput2) is a strong number.") } else { print("NO! (myInput2) is not a strong number.") } Output NO! 345 is not a strong number. YES! 145 is a strong number. Conclusion

Here in the above code, we have two numbers that are 345 and 145. Now we create a function to check if the given numbers are strong numbers or not. In this function, first, we first make a copy of the original number and initialize the sum to 0. Now we run a while loop to repeatedly extract the last of the number and then perform the factorial of that digit and add the factorial result to the sum. Then the function checks if the given number is equal to the sum of the factorial of the digits. If yes then the function returns true. Otherwise, return false.

So this is how we can check if the given number is a strong number or not.

## C# Program To Check If A Path Is A Directory Or A File

Introduction

Let us learn how to write C# program to check if a path is a directory or a file. A directory, also known as a folder, is a place on your computer where you can save files. A directory holds other directories or shortcuts in addition to files.

A file is a collection of data on a drive that has a unique identifier and a directory path. When a file is opened for viewing or writing, it is transformed into a stream. The stream is simply a sequence of bytes traversing the communication route.

Files vs Directory

Files are real data files, whereas directories are repositories for logical file placement on your system. To work with files and directories, the common language runtime (CLR) has the classes File, FileInfo, Directory, and DirectoryInfo in the chúng tôi namespace.

To deal with directories in C#, we can use Directory or DirectoryInfo. Directory class is a static class with static functions for dealing with directories. This class can not be inherited. A DirectoryInfo instance gives information about a specific directory.

There is a file class and a fileinfo class for folders. The File class is used for common tasks such as duplicating, moving, renaming, making, opening, deleting, and adding to a single file. The File class can also be used to get and change file characteristics or DateTime information associated with file creation, access, and writing. Both the File and FileInfo classes have the same fundamental functionality.

The only difference is that the File class has intrinsic methods for dealing with files, whereas the FileInfo class has instance methods for dealing with files.

Public static bool Exists (string? path); is used to check if the file or directory exists. Here the parameter is string? path. Which is the path to check. Its return type is boolean. The reason for this function to be boolean is that when the path is checked then there are only two outcomes. Either the file or directory exists or not, much like the keyword of the function. So here true is returned if there is the existence of the directory or the file and false is returned if it does not exist or any type of error occurs while trying to access the address such as a broken address or more.

Algorithm

The algorithm below will give a step-by-step procedure to write a program to check if the path given is a directory or a file.

Step 1 − Firstly we have to declare a string that contains the address which we want to check whether it is a file or directory

Step 2 − Then there has to be a condition check in which we use the method public static bool Exists (string? path); to check the existence of a file.

Step 3 − It is solely on the programmer to decide what he wants to check first. Whether he wants to check the path as a file or a directory.

Step 4 − If the path fails both of the checks then the output comes as an invalid path and the same is displayed as a message.

Example using System; using System.IO; class ttpt { static void Main() { string PathCheck = “D:/ipl”; if(File.Exists(PathCheck)) { Console.WriteLine(“A file exists on this path”); } else if(Directory.Exists(PathCheck)) { Console.WriteLine(“A directory exists on this path”); } else { Console.WriteLine("{0} is invalid. The input is neither a file nor a directory.", path); } } } Output A file exists on this path

The above-mentioned code is to check whether the path provided is a file or not. In the code first, we have declared a string to store the address to check whether it is a file or a directory. Then we use the public static bool Exists (string? path); which can be used with File as well as Directory class to check the existence of a file or directory by using the respective class keyword. This can be done by using the conditional check. If the person wants to do a bulk check then an array of addresses can be passed as a parameter by making a class. And then check them one by one. As public static bool Exists (string? path); returns a boolean is the reason we are doing the condition check.

Before verifying whether the directory exists, trailing spaces are deleted from the conclusion of the path argument.

The path parameter’s case sensitivity correlates to the file system on which the code is executing. For example, NTFS (the usual Windows file system) is case-insensitive, whereas Linux file systems are case-sensitive.

Time Complexity

In the algorithm after the string is declared. The public static bool Exists (string? path); the method is a boolean returning method. Because it makes a single call straight to the element we are searching for, the algorithm has a time complexity of O(1).

Conclusion

So, we have reached the end of the article and we have learned how to check whether the path provided is the directory or a file. We started with the definition of the file and directory and then we moved on and saw the difference between files and directory. Then we understood the algorithm of the program and after that, we saw the program to check the path. We hope that this article enhances your knowledge regarding C#.

## Golang Program To Calculate The Sum Of Columns Of Matrix Elements

A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array, each of the values of this matrix is known as an element. Here we will use three methods to find the sum of column elements and compare each method for the same using go programming.

Here is an example of a matrix and the sum value of it’s columns −

The given matrix is −

0 1 2 4 5 6 8 9 7

Sum of elements in column 1 is 12

Sum of elements in column 2 is 15

Sum of elements in column 3 is 15

Algorithm

Step 1 − Import the fmt package.

Step 2 − Now we need to start the main() function.

Step 3 − Then we are creating a matrix naming matrix and assign elements to it.

Step 4 − Print the matrix on the screen using fmt.Println() function.

Step 5 − Initialize a new variable called sum of type int to hold the resultant sum.

Step 6 − To find sum of the column elements use the for loop to iterate over the matrix.

Step 7 − Using the first for loop is used to get the column of the matrix while the second for loop gives us the column of the matrix.

Step 8 − Once the loop gets over the matrix elements update the sum variable by adding values to it.

Step 9 − Print the sum of the matrix on the screen.

Example 1

In the following example, we will use a for loop to iterate over the matrix and find the sum of its elements and print it on the screen.

package main import "fmt" func main() { matrix := [][]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 7}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix); i++ { sum := 0 for j := 0; j < len(matrix); j++ { sum += matrix[j][i] } fmt.Printf("Sum of elements in column %d is %dn", i+1, sum) } } Output The given matrix is: 0 1 2 4 5 6 8 9 7 Sum of elements in column 1 is 12 Sum of elements in column 2 is 15 Sum of elements in column 3 is 15 Example 2

In this example we will find the sum of the columns of matrix elements using range function.

package main import "fmt" func main() { matrix := [][]int{ {10, 1, 2}, {4, 50, 6}, {8, 9, 7}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix); i++ { sum := 0 for _, row := range matrix { sum += row[i] } fmt.Printf("Sum of elements in column %d is %dn", i+1, sum) } } Output The given matrix is: 10 1 2 4 50 6 8 9 7 Sum of elements in column 1 is 22 Sum of elements in column 2 is 60 Sum of elements in column 3 is 15 Example 3

In this example we will use the recursion approach to find the sum of columns of matrix elements.

package main import "fmt" func colSum(matrix [][]int, col int) int { if col == len(matrix) { return 0 } sum := 0 for i := range matrix { sum += matrix[i][col] } return sum } func main() { matrix := [][]int{ {20, 1, 22}, {43, 5, 16}, {86, 91, 10}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix); i++ { fmt.Printf("Sum of elements in column %d is %dn", i+1, colSum(matrix, i)) } } Output The given matrix is: 20 1 22 43 5 16 86 91 10 Sum of elements in column 1 is 149 Sum of elements in column 2 is 97 Sum of elements in column 3 is 48 Conclusion

We have successfully compiled and executed a go language program to calculate the sum of columns of matrix elements along with examples. In the first and second example we have used for loop and range functions respectively in the main() section of the program while in the last example we have used a separate function to implement the logic.

Update the detailed information about Swift Program To Check If A Given Square Matrix Is An Identity Matrix on the Daihoichemgio.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!