Mercurial > octave
annotate scripts/special-matrix/magic.m @ 33624:e0c037a01fde default tip
redirect stdout and stderr into experimental terminal widget
* command-widget.cc (command_widget): open temp files for redirecting
stdout and stderr, setup a file system watcher for signals on changes
to these files;
(~command_widget): close and remove the temp. files;
(insert_interpreter_output): use the style as second argument
(print_stream): add new contents of temp. files for stdout and stderr
to the end of the terminal contents;
(notice_settings): set the terminal preferences to the new lexer of
the console;
(console): create and set new lexer;
(new_command_line): use prompt style for the prompt;
(execute_command): use second command line argument for the style;
(append_string): use style as second argument and style the added
text accordingly;
* command-widget.h (console) append_string with second argument for style,
(command_widget): now with destructor, new method print_stream,
insert_interpreter_output with second argument for style, new class
variables for temp. file descriptors and file system watcher
* console-lexer.cc/h: new lexer derived from QScintillas default lexer
for styling the terminal output, styles used so far: Default, Error,
and Prompt
* gui-preferences-cs.h: new constants for error and prompt colors
* libgui/src/module.mk: new files console_lexer.cc/h
* qt-interpreter-events.cc (display_exception): interpreter_output_signal
with second argument for the style, here Error style
* qt-interpreter-events.h: interpreter_output_signal with second
argument for the style
* terminal-dock-widget.h: interpreter_output_signal with second
argument for the style
author | Torsten Lilge <ttl-octave@mailbox.org> |
---|---|
date | Sat, 25 May 2024 14:36:01 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 ## Copyright (C) 1999-2024 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5827 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
13 ## (at your option) any later version. |
5827 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
18 ## GNU General Public License for more details. |
5827 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5827 | 25 |
26 ## -*- texinfo -*- | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {@var{M} =} magic (@var{n}) |
5827 | 28 ## |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## Create an @var{n}-by-@var{n} magic square. |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## A magic square is an arrangement of the integers @code{1:n^2} such that the |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## row sums, column sums, and diagonal sums are all equal to the same value. |
5827 | 33 ## |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21580
diff
changeset
|
34 ## Note: @var{n} must be a scalar greater than or equal to 3. If you supply |
20854
cdf08aacfd86
magic.m: magic(N) accepting all integers (matlab compatibility)
Bernardo Sulzbach <mafagafogigante@gmail.com>
parents:
20852
diff
changeset
|
35 ## @var{n} less than 3, magic returns either a nonmagic square, or else the |
cdf08aacfd86
magic.m: magic(N) accepting all integers (matlab compatibility)
Bernardo Sulzbach <mafagafogigante@gmail.com>
parents:
20852
diff
changeset
|
36 ## degenerate magic squares 1 and []. |
5827 | 37 ## @end deftypefn |
38 | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
39 function M = magic (n) |
5827 | 40 |
28891
de5f2f9a64ff
maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
41 if (nargin < 1) |
5827 | 42 print_usage (); |
43 endif | |
44 | |
20881
7234cbd04292
magic.m:Fix failing tests after cset cdf08aacfd86
Andreas Weber <andy.weber.aw@gmail.com>
parents:
20854
diff
changeset
|
45 n = fix (n); |
25235
27e6b38571d3
magic.m: Issue an error if input N is negative (bug #53441).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
46 if (n < 0) |
27e6b38571d3
magic.m: Issue an error if input N is negative (bug #53441).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
47 error ("magic: N must be non-negative"); |
27e6b38571d3
magic.m: Issue an error if input N is negative (bug #53441).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
48 elseif (n < 1) |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
49 M = []; |
5827 | 50 elseif (mod (n, 2) == 1) |
51 | |
52 shift = floor ((0:n*n-1)/n); | |
53 c = mod ([1:n*n] - shift + (n-3)/2, n); | |
54 r = mod ([n*n:-1:1] + 2*shift, n); | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
55 M(c*n+r+1) = 1:n*n; |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
56 M = reshape (M, n, n); |
5827 | 57 |
58 elseif (mod (n, 4) == 0) | |
59 | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
60 M = reshape (1:n*n, n, n)'; |
5827 | 61 I = [1:4:n, 4:4:n]; |
62 J = fliplr (I); | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
63 M(I,I) = M(J,J); |
5827 | 64 I = [2:4:n, 3:4:n]; |
65 J = fliplr (I); | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
66 M(I,I) = M(J,J); |
5827 | 67 |
68 elseif (mod (n, 4) == 2) | |
69 | |
70 m = n/2; | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
71 M = magic (m); |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
72 M = [M, M+2*m*m; M+3*m*m, M+m*m]; |
5827 | 73 k = (m-1)/2; |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
74 if (k > 1) |
5827 | 75 I = 1:m; |
76 J = [2:k, n-k+2:n]; | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
77 M([I,I+m],J) = M([I+m,I],J); |
5827 | 78 endif |
79 I = [1:k, k+2:m]; | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
80 M([I,I+m],1) = M([I+m,I],1); |
5827 | 81 I = k + 1; |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
82 M([I,I+m],I) = M([I+m,I],I); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 |
5827 | 84 endif |
85 | |
86 endfunction | |
87 | |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
88 |
5827 | 89 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %! for i = 3:30 |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
91 %! A = magic (i); |
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
92 %! assert (norm(diff([sum(diag(A)),sum(diag(flipud(A))),sum(A),sum(A')])),0); |
5827 | 93 %! endfor |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
94 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
95 ## Not a magic square but we must return something (bug #46672). |
20891
95c0d4c07c56
magic: add tests for magic(2) (bug #46672)
Carnë Draug <carandraug@octave.org>
parents:
20881
diff
changeset
|
96 ## While one day we may change the actual return of magic (2), |
95c0d4c07c56
magic: add tests for magic(2) (bug #46672)
Carnë Draug <carandraug@octave.org>
parents:
20881
diff
changeset
|
97 ## this properties still must be true. |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
98 %!test <*46672> |
20891
95c0d4c07c56
magic: add tests for magic(2) (bug #46672)
Carnë Draug <carandraug@octave.org>
parents:
20881
diff
changeset
|
99 %! m = magic (2); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20891
diff
changeset
|
100 %! assert (size (m), [2 2]); |
25235
27e6b38571d3
magic.m: Issue an error if input N is negative (bug #53441).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
101 %! assert (m, [4 3; 1 2]); |
20891
95c0d4c07c56
magic: add tests for magic(2) (bug #46672)
Carnë Draug <carandraug@octave.org>
parents:
20881
diff
changeset
|
102 |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
103 %!assert (isempty (magic (0))) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %!assert (magic (1), 1) |
20881
7234cbd04292
magic.m:Fix failing tests after cset cdf08aacfd86
Andreas Weber <andy.weber.aw@gmail.com>
parents:
20854
diff
changeset
|
105 %!assert (magic (1.5), 1) |
13891
5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
106 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
107 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
108 %!error <Invalid call> magic () |
25235
27e6b38571d3
magic.m: Issue an error if input N is negative (bug #53441).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
109 %!error <N must be non-negative> magic (-5) |