dendrocas 0.1.0 copy "dendrocas: ^0.1.0" to clipboard
dendrocas: ^0.1.0 copied to clipboard

A little CAS for building callable mathematical syntax trees from strings.

example/example.md

Dendrocas Examples #

SVF #

const height = 40, width = 40, y0 = -1, y1 = 1;
int mapY(num y) => ((y - y0) / (y1 - y0) * width).toInt();

final start = '0'.toDouble(),
    end = 'π'.toDouble(),
    by = (end - start) / height,
    f = '0.75 sin(x)'.svf,
    g = 'cos(5x)'.svf;

void plot(List<String> line, int c, String ch) {
  if (c >= 0 && c < line.length) {
    line[c] = ch;
  }
}

for (var x = start; x <= end; x += by) {
  final line = [for (final _ in Iterable.generate(width + 1)) ' '];
  line[mapY(0)] = '|';
  final fc = mapY(f(x)), gc = mapY(g(x));

  if (gc < fc) {
    for (var i = gc; i <= fc; i++) {
      plot(line, i, '▒');
    }
  } else {
    for (var i = fc; i <= gc; i++) {
      plot(line, i, ':');
    }
  }
  print(line.join());
}
                    :::::::::::::::::::::
                    |::::::::::::::::::  
                    | :::::::::::::      
                    |  :::::             
                    ▒▒▒▒▒                
            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒               
     ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒              
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒             
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒            
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒           
     ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒          
            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒         
                    ▒▒▒▒▒▒▒▒▒▒▒▒▒        
                    |      ▒▒▒▒▒▒        
                    |            ::      
                    |            ::::::  
                    |             :::::::
                    |             :::::  
                    |             :      
                    |      ▒▒▒▒▒▒▒▒      
                   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒     
            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒      
     ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒      
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒      
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒      
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒       
     ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒       
            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        
                   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒        
                    |      ▒▒▒▒▒         
                    |         :::::      
                    |        ::::::::::  
                    |       :::::::::::::
                    |      ::::::::::::  
                    |     :::::::::      
                    |    :::             
                    ▒▒▒▒▒                
            ▒▒▒▒▒▒▒▒▒▒▒▒                 
     ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒                  
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒                   

Tree #

final f = 'sqrt(x^2 + x*y + y^2) * logb(2, abs(x) + 2) / 1.5'.tree,
      width = 30,
      from = -3,
      to = 3,
      by = (to - from) / width,
      map = (num t) => (from + t * by).constant,
      ch = (Node result) => switch (result) {
        Constant(value: final v) => switch(v) {
            > 3 => '█',
            > 2.5 => '▓',
            > 1.5 => '▒',
            > 0.5 => '·',
            _ => ' '
          },
        _ => ' ',
      };

print([
  for (var y = 0; y < width; y++)
    [
      for (var x = 0; x < width; x++) 
          ch(f({'x'.variable : map(x), 'y'.variable: map(y)}))
    ].join('')
].join('\n'));
█████████████▓▒▒▒▒▓▓▓▓████████
████████████▓▓▒▒▒▒▒▒▓▓▓▓██████
████████████▓▒▒▒▒▒▒▒▒▓▓▓▓█████
███████████▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓████
██████████▓▓▒▒▒·▒▒▒▒▒▒▒▒▓▓████
██████████▓▒▒▒▒··▒▒▒▒▒▒▒▒▓▓███
█████████▓▓▒▒▒·····▒▒▒▒▒▒▓▓███
█████████▓▒▒▒▒·······▒▒▒▒▒▓███
████████▓▓▒▒▒·········▒▒▒▒▓▓██
███████▓▓▒▒▒··········▒▒▒▒▓▓██
███████▓▒▒▒▒···········▒▒▒▓▓██
██████▓▓▒▒▒············▒▒▒▓███
██████▓▒▒▒·····   ·····▒▒▒▓███
█████▓▓▒▒▒····     ····▒▒▒▓███
█████▓▒▒▒····      ···▒▒▒▓▓███
█████▓▒▒▒····     ····▒▒▒▓████
████▓▓▒▒▒···      ····▒▒▒▓████
████▓▒▒▒····     ····▒▒▒▓▓████
████▓▒▒▒·····   ·····▒▒▒▓█████
████▓▒▒▒············▒▒▒▓▓█████
███▓▓▒▒▒···········▒▒▒▒▓██████
███▓▓▒▒▒▒··········▒▒▒▓▓██████
███▓▓▒▒▒▒·········▒▒▒▓▓███████
████▓▒▒▒▒▒·······▒▒▒▒▓████████
████▓▓▒▒▒▒▒▒·····▒▒▒▓▓████████
████▓▓▒▒▒▒▒▒▒▒··▒▒▒▒▓█████████
█████▓▓▒▒▒▒▒▒▒▒·▒▒▒▓▓█████████
█████▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓██████████
██████▓▓▓▓▒▒▒▒▒▒▒▒▓███████████
███████▓▓▓▓▒▒▒▒▒▒▓▓███████████

SVF Derivative #

const width = 10;

final f = 'ln(sin(x^2))'.svf,
  df = f.derivative,
  from = 'π/6'.toDouble(),
  to = 'π/2'.toDouble(),
  by = (to - from) / 5;

print('f(x) = $f');
print("f'(x) = $df\n");
print(
  '${'x'.padRight(width)}'
  '${'f(x)'.padRight(width)}'
  "f'(x)"
);
print('-' * (3 * width));

for (var x = from; x <= to; x += by) {
  print(
    '${x.toStringAsFixed(4).padRight(width)}'
    '${f(x).toStringAsFixed(4).padRight(width)}'
    '${df(x).toStringAsFixed(4)}'
  );
}

f(x) = log(sin(x^2))
f'(x) = 2*x*cot(x^2)

x         f(x)      f'(x)
------------------------------
0.5236    -1.3066   3.7235
0.7330    -0.6697   2.4606
0.9425    -0.2536   1.5322
1.1519    -0.0300   0.5733
1.3614    -0.0404   -0.7903

Tree Derivative #

const width = 10;
final x = 'x'.variable,
  y = 'y'.variable,
  y0 = 0.2.constant,
  f = 'x^2*y + 3x*y'.tree,
  dfx = f.derivative(x),
  from = '0'.constant,
  to = '1'.constant,
  by = ((to - from) / 5.constant)();

print('f(x, y) = $f');
print('  ∂f/∂x = $dfx');
print('   At y = $y0:\n');

print(
    '${'x'.padRight(width)}'
    '${'f(x, y)'.padRight(width)}'
    '∂f/∂x'
  );
for (var c = from; c <= to; c = (c + by)() as Constant) {
  print(
    '${'$c'.padRight(width)}'
    '${'${f({x: c, y: y0})}'.padRight(width)}'
    '${dfx({x: c, y: y0})}'
  );
}
f(x, y) = 3*x*y+y*x^2
  ∂f/∂x = 2*x*y+3*y
   At y = 0.2:

x         f(x, y)   ∂f/∂x
0         0         0.6
0.2       0.128     0.68
0.4       0.272     0.76
0.6       0.432     0.84
0.8       0.608     0.92
1         0.8       1

Structure #

final a = 'a'.v,
  b = 'b'.v, 
  one = 1.c,
  two = 2.c;

for (final node in [
  -a,
  a + b,
  a * a,
  (a + one).power(two),
  (two * a) / (two + b),
  Logarithm(a * b),
  Sine(a).power(two) + Cosine(a).power(two),
]) {
  print(node);
  print('${node.structure}\n');
  final restructured = node();
  if (restructured != node) {
    print('\nRestructured to $restructured.');
    print(restructured.structure);
  } else {
    print('\n(No restructuring.)');
  }
  print('\n${':' * 30}\n\n');
}
-a
☉ Negative
| ☉ Variable 'a'

(No restructuring.)

::::::::::::::::::::::::::::::

a+b
☉ Sum
| ☉ Variable 'a'
| ☉ Variable 'b'

(No restructuring.)

::::::::::::::::::::::::::::::

a*a
☉ Product
| ☉ Variable 'a'
| ☉ Variable 'a'

Restructured to a^2.
☉ Power
| ☉ Variable 'a'
| ☉ Constant 2

::::::::::::::::::::::::::::::

(1+a)^2
☉ Power
| ☉ Sum
| | ☉ Constant 1
| | ☉ Variable 'a'
| ☉ Constant 2

(No restructuring.)

::::::::::::::::::::::::::::::

2*a/(2+b)
☉ Quotient
| ☉ Product
| | ☉ Constant 2
| | ☉ Variable 'a'
| ☉ Sum
| | ☉ Constant 2
| | ☉ Variable 'b'

Restructured to 2*a*(2+b)^(-1).
☉ Product
| ☉ Constant 2
| ☉ Product
| | ☉ Variable 'a'
| | ☉ Power
| | | ☉ Sum
| | | | ☉ Constant 2
| | | | ☉ Variable 'b'
| | | ☉ Constant -1

::::::::::::::::::::::::::::::

log(a*b)
☉ Logarithm
| ☉ Product
| | ☉ Variable 'a'
| | ☉ Variable 'b'

Restructured to log(a)+log(b).
☉ Sum
| ☉ Logarithm
| | ☉ Variable 'a'
| ☉ Logarithm
| | ☉ Variable 'b'

::::::::::::::::::::::::::::::

cos(a)^2+sin(a)^2
☉ Sum
| ☉ Power
| | ☉ Cosine
| | | ☉ Variable 'a'
| | ☉ Constant 2
| ☉ Power
| | ☉ Sine
| | | ☉ Variable 'a'
| | ☉ Constant 2

Restructured to 1.
☉ Constant 1

::::::::::::::::::::::::::::::

0
likes
160
points
35
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A little CAS for building callable mathematical syntax trees from strings.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on dendrocas