Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,304 questions

42,479 answers

573 users

How to calculate the normal and trace of square matrix in Dart

1 Answer

0 votes
import 'dart:math';

double CalculateNormal(List<List<int>>matrix) {
    var normal = 0;
    
    for (var i = 0; i < matrix.length; i++) {
        for (var j = 0; j < matrix.length; j++) {
            normal += matrix[i][j] * matrix[i][j];
        }
    }
    return sqrt(normal);
}

int CalculateTrace(List<List<int>>matrix) {
    var trace = 0;
    
    for (var i = 0; i < matrix.length; i++) {
        trace += matrix[i][i];
    }
    
    return trace;
}

void main() {
    List<List<int>> matrix = [[1, 1, 1, 1, 1], 
                              [2, 2, 2, 2, 2], 
                              [3, 3, 3, 3, 3], 
                              [4, 4, 4, 4, 4], 
                              [5, 5, 5, 5, 5]];
    
    print(CalculateTrace(matrix));
    
    print(CalculateNormal(matrix));
}





/*
run:

15
16.583123951777

*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Feb 28, 2023 by avibootz

Related questions

...