import 'raya:convert';
enum Size { small, large }
data class Item(final String name, final double price, final Size size, final List<int> tags);
data class Order(final String id, final List<Item> items);
void main() {
final tea = Item('tea', 2.5, Size.large, [1, 2]);
print(tea);
print(tea.copyWith(price: 3.0));
// Collections compare by content, so these are equal.
print(tea == Item('tea', 2.5, Size.large, [1, 2]));
final order = Order('A-17', [tea, Item('cake', 4.0, Size.small, [])]);
final json = jsonEncode(order.toJson());
print(json);
final back = Order.fromJson(jsonDecode(json) as Map<String, Object?>);
print(back == order);
}Output · Runs in your browserItem(name: tea, price: 2.5, size: Size.large, tags: [1, 2])
Item(name: tea, price: 3.0, size: Size.large, tags: [1, 2])
true
{"id":"A-17","items":[{"name":"tea","price":2.5,"size":"large","tags":[1,2]},{"name":"cake","price":4.0,"size":"small","tags":[]}]}
true // Quadrance: the squared distance. For whole-number points it is a whole number.
int quadrance((int, int) a, (int, int) b) {
final dx = b.$1 - a.$1, dy = b.$2 - a.$2;
return dx * dx + dy * dy;
}
// Spread between directions u and v: (u × v)² / (Q(u) Q(v)), as a fraction: 0 for parallel, 1 for perpendicular.
(int, int) spread((int, int) u, (int, int) v) {
final cross = u.$1 * v.$2 - u.$2 * v.$1;
final num = cross * cross;
final den = (u.$1 * u.$1 + u.$2 * u.$2) * (v.$1 * v.$1 + v.$2 * v.$2);
final g = num.gcd(den);
return (num ~/ g, den ~/ g);
}
String frac((int, int) f) => f.$2 == 1 ? '${f.$1}' : '${f.$1}/${f.$2}';
void main() {
const a = (0, 0), b = (4, 0), c = (1, 3);
final q1 = quadrance(b, c), q2 = quadrance(a, c), q3 = quadrance(a, b);
print('quadrances: Q1 = $q1, Q2 = $q2, Q3 = $q3');
final sA = spread((b.$1 - a.$1, b.$2 - a.$2), (c.$1 - a.$1, c.$2 - a.$2));
final sB = spread((a.$1 - b.$1, a.$2 - b.$2), (c.$1 - b.$1, c.$2 - b.$2));
final sC = spread((a.$1 - c.$1, a.$2 - c.$2), (b.$1 - c.$1, b.$2 - c.$2));
print('spreads: s_A = ${frac(sA)}, s_B = ${frac(sB)}, s_C = ${frac(sC)}');
// Spread law: s_A / Q1 = s_B / Q2 = s_C / Q3, checked by cross-multiplying integers.
print('spread law holds exactly: '
'${sA.$1 * sB.$2 * q2 == sB.$1 * sA.$2 * q1 && sB.$1 * sC.$2 * q3 == sC.$1 * sB.$2 * q2}');
// A right angle has spread exactly 1. Perpendicular directions:
print('spread of (3, 4) and (-4, 3): ${frac(spread((3, 4), (-4, 3)))}');
// Parallel directions have spread exactly 0:
print('spread of (2, 6) and (1, 3): ${frac(spread((2, 6), (1, 3)))}');
}Output · Runs in your browserquadrances: Q1 = 18, Q2 = 10, Q3 = 16
spreads: s_A = 9/10, s_B = 1/2, s_C = 4/5
spread law holds exactly: true
spread of (3, 4) and (-4, 3): 1
spread of (2, 6) and (1, 3): 0
import 'package:riptide_numerics_cpu/riptide_numerics_cpu.ray' as np;
import 'package:riptide_numerics_cpu/linalg.ray' as la;
void main() {
final a = np.NDArray.fromList([
[4.0, -2.0, 1.0],
[-2.0, 4.0, -2.0],
[1.0, -2.0, 4.0],
]);
final b = np.NDArray.fromList([11.0, -16.0, 17.0]);
final x = la.solve(a, b); // solve a·x = b
print('x = $x');
print('det = ${la.det(a)}');
final r = la.svd(a); // singular values
print('singular values = ${r.s}');
final e = la.eigh(a); // symmetric eigenvalues
print('eigenvalues = ${e.values}');
}Output · Runs in your browserx = array([1., -2., 3.])
det = array(36.)
singular values = array([7.3722813, 3., 1.6277187])
eigenvalues = array([1.6277187, 3., 7.3722813])
import 'package:riptide_cas/riptide_cas.ray';
void main() {
final x = Sym('x');
final f = Expr.parse('x^3 * sin(x)');
print('f = $f');
print("f' = ${f.diff(x)}");
print('∫ x² dx = ${integrate(Expr.parse('x^2'), x)}');
print('expand = ${Expr.parse('(x + 1)^4').expand()}');
print('factor = ${factor(Expr.parse('x^4 - 1'))}');
print('solve = ${solve(Expr.parse('x^2 - 5*x + 6'), x)}');
print('series = ${series(Expr.parse('exp(x)'), x, n: 5)}');
print('LaTeX = ${f.diff(x).toLatex()}');
}Output · Recorded on the Raya VM, an M1 Pro MacBook Prof = x**3*sin(x)
f' = x**3*cos(x) + 3*x**2*sin(x)
∫ x² dx = x**3/3
expand = x**4 + 4*x**3 + 6*x**2 + 4*x + 1
factor = (x - 1)*(x + 1)*(x**2 + 1)
solve = [2, 3]
series = 1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)
LaTeX = x^{3} \cos{\left(x \right)} + 3 x^{2} \sin{\left(x \right)} import 'raya:ffi';
import 'raya:typed_data';
@Native<Int32 Function()>(symbol: 'raya_matrix_features', isLeaf: true)
external int matrixFeatures();
@Native<Void Function(Pointer<Int8>, Pointer<Int8>, Pointer<Int32>, Int64, Int64, Int64, Int64)>(
symbol: 'raya_matmul_i8', isLeaf: true)
external void matmulI8(Pointer<Int8> a, Pointer<Int8> b, Pointer<Int32> c, int m, int n, int k, int bRows);
@Native<Void Function(Pointer<Int16>, Pointer<Int16>, Pointer<Int64>, Int64, Int64, Int64, Int64)>(
symbol: 'raya_matmul_i16', isLeaf: true)
external void matmulI16(Pointer<Int16> a, Pointer<Int16> b, Pointer<Int64> c, int m, int n, int k, int bRows);
void main() {
final f = matrixFeatures();
print('matrix unit: f32 ${f & 1 != 0}, int8 ${f & 2 != 0}, int16 ${f & 4 != 0}');
// A is 2 x 3, B is 3 x 2.
final a = Int8List.fromList([1, 2, 3, 4, 5, 6]);
final b = Int8List.fromList([7, 8, 9, 10, 11, 12]);
final c = Int32List(4);
matmulI8(a.address, b.address, c.address, 2, 2, 3, 0);
print('int8 A·B = $c');
// int16 in, int64 out: the extreme value squared three times, exact.
final w = Int16List.fromList([-32768, -32768, -32768]);
final big = Int64List(1);
matmulI16(w.address, w.address, big.address, 1, 1, 3, 1);
print('int16 dot = ${big[0]} (expected ${3 * 32768 * 32768})');
}Output · Recorded on the Raya VM, an M1 Pro MacBook Promatrix unit: f32 false, int8 false, int16 false
int8 A·B = [58, 64, 139, 154]
int16 dot = 3221225472 (expected 3221225472)
import 'raya:concurrent';
import 'raya:isolate';
import 'raya:typed_data';
@pragma('vm:shared')
final Int64List hits = Int64List(1);
@pragma('vm:shared')
final Mutex lock = Mutex();
void work(int n) {
for (var i = 0; i < n; i++) {
lock.runLocked(() => hits[0]++);
}
}
Future<void> main() async {
await Future.wait([
for (var i = 0; i < 4; i++) Isolate.run(() => work(1000)),
]);
print('hits: ${hits[0]}'); // one counter, four workers
}Output · Recorded on the Raya VM, an M1 Pro MacBook Prohits: 4000