oak-matlab 0.0.11

MATLAB numerical computing language parser with support for scientific computing, data analysis, and visualization.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
% MATLAB Test File - Comprehensive Syntax Coverage
% This file tests various MATLAB syntax elements for lexer testing

%% Script Header and Documentation
% MATLAB Comprehensive Test Script
% Author: Test Suite
% Date: 2024
% Purpose: Test lexer with various MATLAB syntax elements

%% Clear workspace and command window
clear all; %#ok<CLALL>
close all;
clc;

%% Constants and Basic Variables
% Scalar variables
a = 5;
b = 3.14159;
c = 2.5e-3;
d = 1.23e+10;
e = -42;
f = inf;
g = -inf;
h = NaN;

% Complex numbers
z1 = 3 + 4i;
z2 = 2 + 3j;
z3 = complex(1, 2);

% Logical variables
flag1 = true;
flag2 = false;
flag3 = logical(1);

% String and character arrays
str1 = 'Hello World';
str2 = "Modern string";
char_array = ['a', 'b', 'c'];

%% Arrays and Matrices
% Row vectors
row_vec = [1, 2, 3, 4, 5];
row_vec2 = [1 2 3 4 5];

% Column vectors
col_vec = [1; 2; 3; 4; 5];

% Matrices
matrix2x3 = [1, 2, 3; 4, 5, 6];
matrix3x3 = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% Special matrices
zeros_mat = zeros(3, 4);
ones_mat = ones(2, 5);
eye_mat = eye(4);
rand_mat = rand(3, 3);
randn_mat = randn(2, 4);

% Array indexing and slicing
element = matrix3x3(2, 3);
row_slice = matrix3x3(2, :);
col_slice = matrix3x3(:, 1);
submatrix = matrix3x3(1:2, 2:3);

% Linear indexing
linear_element = matrix3x3(5);

%% Cell Arrays
% Cell array creation
cell_array = {1, 'hello', [1, 2, 3], true};
mixed_cell = {'string', 42, rand(2,2), {1, 2}};

% Cell array indexing
cell_content = cell_array{1};
cell_reference = cell_array(1);

%% Structures
% Structure creation
person.name = 'John Doe';
person.age = 30;
person.height = 5.9;
person.married = true;

% Nested structures
company.name = 'TechCorp';
company.employees(1).name = 'Alice';
company.employees(1).position = 'Engineer';
company.employees(2).name = 'Bob';
company.employees(2).position = 'Manager';

% Dynamic field names
field_name = 'dynamic_field';
person.(field_name) = 'dynamic value';

%% Arithmetic Operations
% Basic arithmetic
sum_result = a + b;
diff_result = a - b;
prod_result = a * b;
div_result = a / b;
power_result = a ^ b;
mod_result = mod(a, 3);

% Element-wise operations
vec1 = [1, 2, 3];
vec2 = [4, 5, 6];
elem_mult = vec1 .* vec2;
elem_div = vec1 ./ vec2;
elem_power = vec1 .^ vec2;

% Matrix operations
mat1 = [1, 2; 3, 4];
mat2 = [5, 6; 7, 8];
mat_mult = mat1 * mat2;
mat_transpose = mat1';
mat_inverse = inv(mat1);
mat_determinant = det(mat1);

%% Logical Operations
% Logical operators
and_result = true && false;
or_result = true || false;
not_result = ~true;

% Element-wise logical operations
logical_vec1 = [true, false, true];
logical_vec2 = [false, true, true];
elem_and = logical_vec1 & logical_vec2;
elem_or = logical_vec1 | logical_vec2;
elem_not = ~logical_vec1;

% Comparison operations
comp1 = a > b;
comp2 = a < b;
comp3 = a >= b;
comp4 = a <= b;
comp5 = a == b;
comp6 = a ~= b;

%% Control Flow Structures

% If-else statements
if a > b
    disp('a is greater than b');
elseif a < b
    disp('a is less than b');
else
    disp('a equals b');
end

% Switch statement
switch a
    case 1
        disp('a is 1');
    case {2, 3, 4}
        disp('a is 2, 3, or 4');
    case 5
        disp('a is 5');
    otherwise
        disp('a is something else');
end

% For loops
for i = 1:10
    fprintf('Iteration %d\n', i);
end

for i = 1:2:10  % Step size of 2
    fprintf('Odd iteration %d\n', i);
end

for i = [1, 3, 5, 7, 9]
    fprintf('Array iteration %d\n', i);
end

% Nested for loops
for i = 1:3
    for j = 1:3
        fprintf('i=%d, j=%d\n', i, j);
    end
end

% While loop
counter = 1;
while counter <= 5
    fprintf('While counter: %d\n', counter);
    counter = counter + 1;
end

% Break and continue
for i = 1:10
    if i == 3
        continue;
    end
    if i == 8
        break;
    end
    fprintf('Loop value: %d\n', i);
end

%% Function Definitions

% Simple function
function result = add_numbers(x, y)
    result = x + y;
end

% Function with multiple outputs
function [sum_val, diff_val, prod_val] = basic_operations(x, y)
    sum_val = x + y;
    diff_val = x - y;
    prod_val = x * y;
end

% Function with variable arguments
function result = sum_all(varargin)
    result = 0;
    for i = 1:nargin
        result = result + varargin{i};
    end
end

% Function with default arguments
function result = power_function(base, exponent)
    if nargin < 2
        exponent = 2;  % Default exponent
    end
    result = base ^ exponent;
end

% Anonymous functions
square = @(x) x.^2;
add_func = @(x, y) x + y;
complex_func = @(x) sin(x) + cos(x);

%% Mathematical Functions
% Trigonometric functions
angle = pi/4;
sin_val = sin(angle);
cos_val = cos(angle);
tan_val = tan(angle);
asin_val = asin(0.5);
acos_val = acos(0.5);
atan_val = atan(1);

% Hyperbolic functions
sinh_val = sinh(1);
cosh_val = cosh(1);
tanh_val = tanh(1);

% Exponential and logarithmic functions
exp_val = exp(1);
log_val = log(10);
log10_val = log10(100);
log2_val = log2(8);

% Power and root functions
sqrt_val = sqrt(16);
power_val = power(2, 3);
nthroot_val = nthroot(27, 3);

% Rounding functions
ceil_val = ceil(3.7);
floor_val = floor(3.7);
round_val = round(3.7);
fix_val = fix(-3.7);

%% String Operations
% String concatenation
str_concat1 = [str1, ' ', str2];
str_concat2 = strcat(str1, str2);

% String comparison
str_equal = strcmp(str1, str2);
str_equal_ignore_case = strcmpi(str1, str2);

% String searching
str_find = strfind(str1, 'World');
str_contains = contains(str1, 'Hello');

% String replacement
str_replace = strrep(str1, 'World', 'MATLAB');

% String conversion
num_str = num2str(42);
str_num = str2num('42');

%% File I/O Operations
% File writing
filename = 'test_output.txt';
fid = fopen(filename, 'w');
if fid ~= -1
    fprintf(fid, 'Hello MATLAB\n');
    fprintf(fid, 'Number: %d\n', 42);
    fprintf(fid, 'Float: %.2f\n', 3.14159);
    fclose(fid);
end

% File reading
if exist(filename, 'file')
    fid = fopen(filename, 'r');
    if fid ~= -1
        file_content = fread(fid, '*char')';
        fclose(fid);
        disp('File content:');
        disp(file_content);
    end
end

% CSV operations
data_matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];
csvwrite('data.csv', data_matrix);
loaded_data = csvread('data.csv');

%% Plotting and Graphics
% Basic 2D plot
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

figure(1);
plot(x, y1, 'r-', x, y2, 'b--');
xlabel('x');
ylabel('y');
title('Sine and Cosine Functions');
legend('sin(x)', 'cos(x)');
grid on;

% Subplot
figure(2);
subplot(2, 2, 1);
plot(x, y1);
title('Sine');

subplot(2, 2, 2);
plot(x, y2);
title('Cosine');

subplot(2, 2, 3);
plot(x, tan(x));
title('Tangent');
ylim([-5, 5]);

subplot(2, 2, 4);
plot(x, exp(-x/2) .* sin(x));
title('Damped Sine');

% 3D plotting
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X.^2 + Y.^2;

figure(3);
surf(X, Y, Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Surface Plot');
colorbar;

% Histogram
random_data = randn(1000, 1);
figure(4);
histogram(random_data, 30);
title('Histogram of Random Data');
xlabel('Value');
ylabel('Frequency');

%% Signal Processing
% Generate signals
fs = 1000;  % Sampling frequency
t = 0:1/fs:1-1/fs;  % Time vector
f1 = 50;  % Frequency 1
f2 = 120; % Frequency 2

signal = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t) + 0.1*randn(size(t));

% FFT analysis
Y = fft(signal);
P2 = abs(Y/length(signal));
P1 = P2(1:length(signal)/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = fs*(0:(length(signal)/2))/length(signal);

figure(5);
subplot(2, 1, 1);
plot(t(1:100), signal(1:100));
title('Time Domain Signal');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
plot(f, P1);
title('Frequency Domain');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

%% Linear Algebra Operations
% Matrix operations
A = [1, 2, 3; 4, 5, 6; 7, 8, 10];
b = [1; 2; 3];

% Solve linear system Ax = b
x_solution = A \ b;

% Eigenvalues and eigenvectors
[eigenvectors, eigenvalues] = eig(A);

% Singular Value Decomposition
[U, S, V] = svd(A);

% QR decomposition
[Q, R] = qr(A);

% LU decomposition
[L, U_lu, P] = lu(A);

%% Statistics and Probability
% Basic statistics
data = randn(100, 1);
mean_val = mean(data);
median_val = median(data);
std_val = std(data);
var_val = var(data);
min_val = min(data);
max_val = max(data);

% Correlation and covariance
data2 = randn(100, 1);
correlation = corrcoef(data, data2);
covariance = cov(data, data2);

% Probability distributions
normal_samples = normrnd(0, 1, [100, 1]);
uniform_samples = unifrnd(0, 1, [100, 1]);
exponential_samples = exprnd(1, [100, 1]);

%% Object-Oriented Programming
% Class definition (would typically be in separate file)
classdef SimpleClass < handle
    properties
        value
        name
    end
    
    properties (Access = private)
        private_data
    end
    
    methods
        function obj = SimpleClass(initial_value, initial_name)
            if nargin > 0
                obj.value = initial_value;
            end
            if nargin > 1
                obj.name = initial_name;
            end
        end
        
        function result = getValue(obj)
            result = obj.value;
        end
        
        function setValue(obj, new_value)
            obj.value = new_value;
        end
        
        function display_info(obj)
            fprintf('Name: %s, Value: %g\n', obj.name, obj.value);
        end
    end
    
    methods (Static)
        function result = static_method(x, y)
            result = x + y;
        end
    end
end

%% Error Handling
% Try-catch blocks
try
    risky_operation = 1 / 0;
    disp('This should not print');
catch ME
    fprintf('Error caught: %s\n', ME.message);
    fprintf('Error identifier: %s\n', ME.identifier);
end

% Custom error throwing
function validate_input(x)
    if x < 0
        error('Input must be non-negative');
    end
    if ~isnumeric(x)
        error('Input must be numeric');
    end
end

%% Advanced Features

% Parallel computing (if Parallel Computing Toolbox available)
if license('test', 'Distrib_Computing_Toolbox')
    parfor i = 1:10
        parallel_result(i) = expensive_computation(i);
    end
end

function result = expensive_computation(n)
    result = sum(1:n);
    pause(0.1);  % Simulate computation time
end

% GPU computing (if Parallel Computing Toolbox available)
if license('test', 'Distrib_Computing_Toolbox') && gpuDeviceCount > 0
    gpu_array = gpuArray([1, 2, 3, 4, 5]);
    gpu_result = gpu_array .^ 2;
    cpu_result = gather(gpu_result);
end

% Symbolic math (if Symbolic Math Toolbox available)
if license('test', 'Symbolic_Toolbox')
    syms x y z;
    symbolic_expr = x^2 + y^2 + z^2;
    symbolic_diff = diff(symbolic_expr, x);
    symbolic_int = int(sin(x), x);
end

%% Performance Timing
% Timing code execution
tic;
large_matrix = rand(1000, 1000);
large_result = large_matrix * large_matrix;
elapsed_time = toc;
fprintf('Matrix multiplication took %.4f seconds\n', elapsed_time);

% Profiling
profile on;
for i = 1:1000
    temp_result = sin(i) + cos(i);
end
profile off;
profile viewer;

%% Memory Management
% Check memory usage
memory_info = memory;
fprintf('Available memory: %.2f GB\n', memory_info.MemAvailableAllArrays / 1e9);

% Clear specific variables
clear temp_result large_matrix large_result;

% Pack memory (remove fragmentation)
pack;

%% System Information
% MATLAB version and system info
matlab_version = version;
computer_type = computer;
java_version = version('-java');

fprintf('MATLAB Version: %s\n', matlab_version);
fprintf('Computer Type: %s\n', computer_type);
fprintf('Java Version: %s\n', java_version);

%% Final Cleanup
% Close all figures
close all;

% Display completion message
fprintf('\n=== MATLAB Comprehensive Test Completed ===\n');
fprintf('All syntax elements have been tested.\n');
fprintf('Check workspace for created variables and results.\n');

%% End of script