Enter forum description here ...
+1
Completed

Sparse Matrices Basic Support - II

Pavel Holoborodko 12 years ago updated 10 years ago 2


1. Sparse-specific function: sparse(), nnz(), find().

2. Indexing & basic manipulation functions - subsref, subsasgn, size, numel, find, etc.

3. Element-wise operations (arithmetic and functions).

4. Matrix operations (matrix-scalar, matrix-vector, matrix-matrix).





0
Fixed

backslash

dattorro 2 months ago updated by Pavel Holoborodko 1 month ago 3

Backslash, applied to an overdetermined system, is expected deterministic; i.e, no variability in solution.

But repeated calls to backslash, for data vL provided in the link, is variable and of infinite dynamic range.

clear all load vL % vL = double(vL); % Amat = double(Amat); xi = Amat\vL;  %backslash error is nonzero when differenced with same backslash later on for i=1:10    notabug = Amat\vL;    sum(abs(xi - notabug)) end

ans = 0

ans = 0
ans = 0
ans = 3.312601991721914126549721394248086e-06
ans = 3.271854404182205550123018444983506e-05
ans = 0
ans = 8.904811628089073569772503832159206e-06
ans = 8.904811628089073569772503832159206e-06
ans = 8.904811628089073569772503832159206e-06
ans = 3.312601991721914126549721394248086e-06

Uncommenting the double cast always produces ans=0 but reveals Amat to be an overdetermined rank deficient matrix.

MATLAB Version: 9.14.0.2489007 (R2023a) Update 6
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 19043)

CPU: AMD 3995WX

Advanpix Multiprecision Computing Toolbox Version 5.2.8.15537

https://www.convexoptimization.com/vL.mat

Answer
Pavel Holoborodko 1 month ago

Jon, Happy New Year!

We have just released new version 5.3.6.15902: https://bit.ly/3mvxXqg
Now, its least-squares solver generates bitwise deterministic results every time.

Would appreciate it if you would update me on your test results.


Repeated runs of the same problem in the same environment will yield the same bitwise result unless something in the environment changes; e.g, CPU type, number of threads,...

I think with more advanced algorithms and many-core CPUs this will change in the future.

For example. Even though we use the same number of threads in a solver - it might split the overall work in different number  of jobs and distribute them differently among cores on each call. This might happen because some CPU cores might be busy with other processes on the computer. More flexible/adaptive algorithms take this into account (as we did in least-square solver before 5.3.6).

Thank you for all your support and contributions!

Best wishes for the New Year!

Pavel

0
Not a bug

randn() precision limited to 55 digits

dattorro 3 months ago updated 2 weeks ago 22

%randn ignores high precision

mp.Digits(1034)

t=randn(1,'mp')
t =0.1048747160164939795645722142580780200660228729248046875

t - 0.1048747160164939795645722142580780200660228729248046875
ans = 0

MATLAB Version 9.14 (R2023a)
Advanpix Multiprecision Computing Toolbox Version 5.2.8.15537

Answer
Pavel Holoborodko 3 months ago

The rand(...,'mp') generates numbers in double precision and then converts them to mp. Hence digits are not filled up to the 1034th digit. We do this in order to produce the same random numbers as built-in MATLAB's rand function (for reproducibility).

If you want random numbers to the full precision (all 1034 digits are filled), please use mprand/mprandn.

>> mp.Digits(100);
>> randn(1,'mp')
ans =
0.8621733203681205548463140075909905135631561279296875

>> mprandn(1,'mp')
ans =
0.1868724737876430395187949415904261118721051078748075419355524734700877850288747581541290481709356331
0
Fixed

Error in fsolve

rdlp 9 months ago updated by Pavel Holoborodko 9 months ago 1

Running your example function for fsolve I get the following error:


Not enough input arguments.

Error in validateFinDiffRelStep (line 10)
value = optimget(options,'FinDiffRelStep',defaultopt,optimgetFlag);

Error in mpfsolve

Error in mp/fsolve (line 5717)
[varargout{1:nargout}] = mpfsolve(varargin{:});

Error in test_fsolve (line 7)
[x,fval,exitflag,output] = fsolve(@myfun,x0,options)

test_fsolve is directly copied from your documentation:

function test_fsolvex0 = mp([-5; -5]);

options = optimset('Display','iter', 'TolFun', mp('eps'),... 'TolX', mp('eps'),... 'Algorithm','levenberg-marquardt'); [x,fval,exitflag,output] = fsolve(@myfun,x0,options) 

function F = myfun(x) 

F = [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))]; 

end

end

My matlab version is 2023b version 8.

Answer
Pavel Holoborodko 9 months ago

Thank you very much for the bug report!

Since R2023b the 'validateFinDiffRelStep' has been updated to accept 4 arguments instead of 3 (as it was before).

Indeed this incompatibility has slipped through our tests.

The toolbox has been updated with the incompatibility fixed. Please re-download & update the toolbox.

We haven't changed the toolbox version number (it is still 5.2.8.15537) but relevant files have been replaced and updated.

0
Fixed

spdiags with low number of digits

michaloutrata 1 year ago updated by Pavel Holoborodko 1 year ago 8

In a larger code I want to construct an mp sparse matrix in two steps - first the off-diagonal entries and then add the diagonal.

However, using mp.Digits(1) the code breaks down with the error message

" Error using mp/sparse
Index exceeds matrix dimensions.

Error in mpspdiags

Error in mp/spdiags (line 4541)
[varargout{1:nargout}] = mpspdiags(varargin{:}); "

I tried to simplify the matrix creation process and got to the following:

mp.Digits(1);

x = zeros(n,'mp');
identity = spdiags(ones(n,1,'mp'),0,x);

This runs fine for n=10, 100 but breaks down for n=101 and many others (with the error message above). Taking mp.Digits(2); and n=1311 or n = 2520 reproduces the same error message. So does mp.Digits(3); and n=4140.

0
Answered

super quick reversion to double

dattorro 2 years ago updated by Pavel Holoborodko 2 years ago 3

Is there a quick & simple but revertible way to convert an extensive Matlab program, developed in multiprecision, back to double precision?  For example,  mp.Digits(16) ?  

This question arises because code is sometimes developed in multiprecision for the purpose of eliminating quantization artifacts (finite precision) as a possible error source.  A natural question arises as to the exact tradeoff of speed versus precision.  This information is required for coherently answering one's boss during an entire development phase, preparing talks & papers, and otherwise impressing people at Mathworks enough for them to integrate MCT into Matlab. 

0
Completed

latest version for Linux

Michal Kvasnicka 2 years ago updated by Pavel Holoborodko 2 years ago 1

Is the version 4.9.1.14792 available for linux?

I am able to download only version 4.9.0.14753

0
Answered

execution time not proportional to array length

dattorro 3 years ago updated by Pavel Holoborodko 3 years ago 1

Pavel,

Doubling array size quadruples execution time.

I'm not asking for help debugging code; this is something you need to see.

Look at it under the Matlab Profiler.

Jon

______________________________________________________________________

%MP Toolbox timing anomaly: twice the array size takes four times the execution time

clearvars

Fs = mp('48000');

freq = mp('997');

NumSecs = mp('1');  %record duration in seconds

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N = NumSecs*Fs;

disp(['generating ' num2str(double(NumSecs)) '-second D/A signal'])

y1 = zeros(N,1,'mp');

y2 = zeros(N,1,'mp');

cosom = cos(mp('2*pi')*freq/Fs);

tic

for n=2:N  %for loop necessary in production code

   y1(n) = cosom*y1(n-1);  %7.5 seconds in Matlab Profiler, AMD 3995WX CPU Windows 10

   y2(n) = cosom*y2(n-1);  %7.5 seconds in Matlab Profiler, AMD 3995WX CPU Windows 10

end

toc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% DOUBLING number of array elements takes FOUR times the previous execution time %%%

NumSecs = mp('2');  %record duration in seconds

%%%%%%%%%%%%%%%%% EXACT DUPLICATE (cut & paste) of code block above %%%%%%%%%%%%%%%%

N = NumSecs*Fs;

disp(['generating ' num2str(double(NumSecs)) '-second D/A signal'])

y1 = zeros(N,1,'mp');

y2 = zeros(N,1,'mp');

cosom = cos(mp('2*pi')*freq/Fs);

tic

for n=2:N  %for loop necessary in production code

   y1(n) = cosom*y1(n-1);  %37 seconds in Matlab Profiler, AMD 3995WX CPU Windows 10

   y2(n) = cosom*y2(n-1);  %37 seconds in Matlab Profiler, AMD 3995WX CPU Windows 10

end

toc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

0
Not a bug

subroutine precision

dattorro 3 years ago updated by Pavel Holoborodko 3 years ago 4

Quoting this page:

https://www.advanpix.com/2011/11/07/gauss-kronrod-quadrature-nodes-weights/

"If input parameters a and b are multiprecision type, then all subsequent calculations inside the function will be conducted with high precision,..."


Re: Matlab 2021a under Windows 10:
User-written subroutines are executed in quadruple precision (34 digits) regardless of number of digits possessed by arguments passed to the subroutine.

For example: If arg1 has 100-digit precision and is passed to subroutine1(arg1), then computations within subroutine1 will be performed in 34-digit precision unless the following statement appears inside:

mp.Digits(mp.Digits(arg1));


0
Fixed

R2022a incompatibility

Michal Kvasnicka 3 years ago updated by Pavel Holoborodko 3 years ago 6

>> mp.Test

nthroot() :Error using char/eps

Too many input arguments.

Error in nthroot (line 35)

m = x ~= 0 & (abs(x) < 1./eps('like',y)) & isfinite(n);

Error in mptest (line 1298)

d = nthroot(X,Y)-nthroot(mp(X),mp(Y));

Error in mp.Test (line 300)

mptest(); % Run tests

Answer
Pavel Holoborodko 3 years ago

I see, this was helpful, thank you!

Now situation is clear. MATLAB R2022a introduced incompatibility with previous versions of MATLAB. 

The issue is - R2022a chooses wrong function overload. MCT provides custom overload for 'eps' function which accepts only one argument. Two-argument calls must be re-directed to the built-in 'eps'. All previous versions of MATLAB handled this correctly. But starting from R2022a, MATLAB (incorrectly) tries to use one-argument overload for two-argument calls.

Obviously this leads to error "Too many input arguments".

Actually this issue is a bug. Overloads look-up rules are quite clear and strict in the world of OOP, it cannot be changed on a whim. Lots of existing MATLAB code might be broken because of this change.

  

We will add workaround for this issue to MCT, so that our overload will handle one and two-argument calls.

At the moment, please replace the content of [toolbox_dir]/@char/eps.m with the following code:

function r = eps( varargin )
    if strcmpi('mp',varargin{end}) || strcmpi('mp',class(varargin{end}))
r = mpimpl(2000);
else
r = builtin('eps',varargin{:});
end end

Let me know if this fixes the issue.