(function() {
    'use strict';

    angular
        .module('hadar.common.services.solver', [])
        .factory('haSolver', haSolverFactory);

    haSolverFactory.$inject = ['$q'];
    function haSolverFactory ($q) {
        var _plalib;
        var _currentProblem;

        var service = {
            getCurrentProblem: getCurrentProblem,
            hasCurrentProblem: hasCurrentProblem,
            cancelCurrentProblem: cancelCurrentProblem,
            gaussJordanElimination: gaussJordanElimination
        };

        return service;

        //////////


        function getCurrentProblem() {
            return _currentProblem;
        }

        function hasCurrentProblem() {
            return !!_currentProblem;
        }

        function cancelCurrentProblem() {
            _currentProblem = null;
            // TODO: Implement problem canceling
            console.log('Cancel current problem');
        }

        function gaussJordanElimination(matrix, vector) {
            var a = _convertMatrixToSharedArray(matrix);
            var b = _convertMatrixToSharedArray(vector);
            var n = matrix.rows;
            var startTime = window.performance.now();
            _currentProblem = _getPlalibInstance()
                .gaussJordanEliminationPar(n, a, b)
                .then(function() {
                    var endTime = window.performance.now();
                    return {
                        solution: b.join('\n'),
                        time: (endTime - startTime) / 1000
                    };
                });
        }

        function _getPlalibInstance () {
            _plalib = _plalib || new Plalib({
                workerUrl: '../node_modules/plalib/dist/plalib-worker.js'
            });
            return _plalib;
        }

        function _convertMatrixToSharedArray(matrix) {
            var numberOfValues = matrix.rows * matrix.columns;
            var matrixBuffer = new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * numberOfValues);
            var regularArray = matrix.values.trimRight().split('\n').map(function(row) {
                return parseInt(row, 10);
            });
            var matrixArray = new Float64Array(matrixBuffer);
            if (matrix.matrixType === 'DENSE') {
                for (var i = 0; i < regularArray.length; i += 1) {
                    matrixArray[i] = regularArray[_byColumnToByRowIndex(i, matrix.columns)];
                }
            } else {
                // TODO: Implement sparse matrix creation
                throw new Error('Sparse matrix is not ready to use');
            }
            return matrixArray;
        }

        function _byColumnToByRowIndex(i, n) {
            return (i % n) * n + Math.floor(i / n);
        }
    }
}());

@@ -1837,24 +1837,1153 @@
 y(matrix) %7B%0A
+            switch(matrix.matrixType) %7B%0A                case 'DENSE': return _convertDenseMatrixToSharedArray(matrix);%0A                case 'SPARSE': return _convertSparseMatrixToSharedArray(matrix);%0A                default: throw new Error('Unsupported matrix type');%0A            %7D%0A        %7D%0A%0A        function _convertSparseMatrixToSharedArray(matrix) %7B%0A            var numberOfValues = matrix.rows * matrix.columns;%0A            var matrixBuffer = new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * numberOfValues);%0A            var matrixArray = new Float64Array(matrixBuffer);%0A            matrix.values.trimRight().split('%5Cn').map(function(row) %7B%0A                return row.split(' ').map(function(value) %7B%0A                    return parseInt(value, 10);%0A                %7D);%0A            %7D).forEach(function(valueRow) %7B%0A                var i = valueRow%5B0%5D - 1;%0A                var j = valueRow%5B1%5D - 1;%0A                var value = valueRow%5B2%5D;%0A                matrixArray%5Bi * matrix.columns + j%5D = value;%0A            %7D);%0A%0A            return matrixArray;%0A        %7D%0A%0A        function _convertDenseMatrixToSharedArray(matrix) %7B%0A
             
@@ -3025,32 +3025,32 @@
 matrix.columns;%0A
-
             var 
@@ -3128,32 +3128,94 @@
 umberOfValues);%0A
+            var matrixArray = new Float64Array(matrixBuffer);%0A
             var 
@@ -3349,123 +3349,8 @@
 %7D);%0A
-            var matrixArray = new Float64Array(matrixBuffer);%0A            if (matrix.matrixType === 'DENSE') %7B%0A    
     
@@ -3404,28 +3404,24 @@
 ; i += 1) %7B%0A
-    
             
@@ -3513,176 +3513,10 @@
     
-    %7D%0A            %7D else %7B%0A                // TODO: Implement sparse matrix creation%0A                throw new Error('Sparse matrix is not ready to use');%0A            
 %7D
+%0A
 %0A   

Add sparse matrix parsing