Mercurial > matrix-functions
annotate toolbox/chop.m @ 2:c124219d7bfa draft
Re-add the 1995 toolbox after noticing the statement in the ~higham/mctoolbox/ webpage.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Thu, 07 May 2015 18:36:24 +0200 |
parents | 8f23314345f4 |
children |
rev | line source |
---|---|
0
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
1 function c = chop(x, t) |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
2 %CHOP Round matrix elements. |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
3 % CHOP(X, t) is the matrix obtained by rounding the elements of X |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
4 % to t significant binary places. |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
5 % Default is t = 24, corresponding to IEEE single precision. |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
6 |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
7 if nargin < 2, t = 24; end |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
8 [m, n] = size(x); |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
9 |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
10 % Use the representation: |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
11 % x(i,j) = 2^e(i,j) * .d(1)d(2)...d(s) * sign(x(i,j)) |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
12 |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
13 % On the next line `+(x==0)' avoids passing a zero argument to LOG, which |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
14 % would cause a warning message to be generated. |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
15 |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
16 y = abs(x) + (x==0); |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
17 e = floor(log2(y) + 1); |
8f23314345f4
Create local repository for matrix toolboxes. Step #0 done.
Antonio Pino Robles <data.script93@gmail.com>
parents:
diff
changeset
|
18 c = pow2(round( pow2(x, t-e) ), e-t); |