herta
Version:
Advanced mathematics framework for scientific, engineering, and financial applications
1,363 lines (1,043 loc) • 83.3 kB
Markdown
# Herta.js
[](https://www.npmjs.com/package/herta)
[](LICENSE)
[]()
[]()
[]()
[]()
[]()
[]()
[]()
[]()
An advanced mathematics framework for Node.js providing powerful tools for mathematical computation, symbolic mathematics, and scientific computing. Designed specifically for scientists, researchers, and advanced mathematical applications.
## Framework Architecture
Herta.js is organized in a modular, intuitive folder structure for better organization and easier navigation:
```
/src
├── core/ # Core mathematical operations
├── algebra/ # Algebraic operations
├── calculus/ # Calculus operations
├── discrete/ # Discrete mathematics (including graph theory)
├── statistics/ # Statistical operations
├── geometry/ # Geometric operations
├── optimization/# Optimization algorithms
├── physics/ # Physics models and simulations
├── crypto/ # Cryptography algorithms
├── utils/ # Utility functions
├── applied/ # Applied mathematics
└── advanced/ # Advanced specialized modules
```
## Feature Highlights
### Units Conversion System
Complete system for converting between various units of measurement:
```javascript
// Convert between different length units
const meters = herta.utils.units.length(5, 'feet', 'meter'); // 1.524 meters
const miles = herta.utils.units.length(10, 'kilometer', 'mile'); // 6.21371 miles
// Temperature conversion
const fahrenheit = herta.utils.units.temperature(100, 'celsius', 'fahrenheit'); // 212°F
// Other unit types
const kilograms = herta.utils.units.mass(16, 'ounce', 'kilogram'); // 0.45359 kg
const seconds = herta.utils.units.time(2, 'hour', 'second'); // 7200 seconds
const sqMeters = herta.utils.units.area(1, 'acre', 'squareMeter'); // 4046.86 m²
const joules = herta.utils.units.energy(100, 'calorie', 'joule'); // 418.4 joules
```
### Advanced Fraction Operations
Full-featured fraction class with arithmetic and comparison methods:
```javascript
const { Fraction } = herta.core.fraction;
// Create fractions
const frac1 = new Fraction(3, 4); // 3/4
const frac2 = new Fraction(2, 5); // 2/5
// Arithmetic operations
const sum = frac1.add(frac2); // 23/20
const difference = frac1.subtract(frac2); // 7/20
const product = frac1.multiply(frac2); // 6/20 (simplified to 3/10)
const quotient = frac1.divide(frac2); // 15/8
// Comparison methods
frac1.equals(new Fraction(6, 8)); // true (both simplify to 3/4)
frac1.greaterThan(frac2); // true
frac2.lessThan(frac1); // true
// Conversions
frac1.toDecimal(); // 0.75
frac1.toString(); // "3/4"
// Create fractions from decimals
const frac3 = Fraction.fromDecimal(0.333333); // Approximates to 1/3
```
### Random Number Generation
Enhanced random number generation with multiple probability distributions:
```javascript
// Basic random generation
const randomInteger = herta.utils.random.randomInt(1, 100); // Random integer between 1-100
const randomDecimal = herta.utils.random.randomFloat(0, 1); // Random float between 0-1
const randomTrueOrFalse = herta.utils.random.randomBoolean(0.7); // 70% chance of true
// Random selections
const array = [10, 20, 30, 40, 50];
const randomElement = herta.utils.random.randomItem(array); // Random item from array
const shuffledArray = herta.utils.random.shuffle(array); // Randomly shuffled array
// Advanced distributions
const normalRandom = herta.utils.random.randomNormal(50, 10); // Normal distribution (μ=50, σ=10)
const exponentialRandom = herta.utils.random.randomExponential(2); // Exponential distribution (λ=2)
const poissonRandom = herta.utils.random.randomPoisson(5); // Poisson distribution (λ=5)
// Other utilities
const uuid = herta.utils.random.uuid(); // Generate UUID v4
const randomHexColor = herta.utils.random.randomColor(); // Random hex color like #FF5733
```
### Mathematical Sequence Generators
Functions for generating mathematical sequences:
```javascript
// Common number sequences
const fibSeq = herta.utils.generators.fibonacci(10); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
const primeSeq = herta.utils.generators.primes(20); // [2, 3, 5, 7, 11, 13, 17, 19]
const triangular = herta.utils.generators.triangularNumbers(7); // [1, 3, 6, 10, 15, 21, 28]
const squares = herta.utils.generators.squareNumbers(5); // [1, 4, 9, 16, 25]
// Pattern-based sequences
const arithmetic = herta.utils.generators.arithmeticSequence(2, 3, 5); // [2, 5, 8, 11, 14]
const geometric = herta.utils.generators.geometricSequence(1, 2, 6); // [1, 2, 4, 8, 16, 32]
// Advanced mathematical sequences
const pascalTri = herta.utils.generators.pascalsTriangle(4); // Triangle with 4 rows
const catalan = herta.utils.generators.catalanNumbers(6); // [1, 1, 2, 5, 14, 42]
const collatz = herta.utils.generators.collatzSequence(12); // [12, 6, 3, 10, 5, 16, 8, 4, 2, 1]
```
### Graph Theory Module
The graph module has been completely rewritten with enhanced functionality and performance:
```javascript
// Create a new graph (directed or undirected)
const graph = new herta.discrete.graph.Graph(true); // directed graph
// Add vertices and weighted edges
graph.addVertex('A', { label: 'Start' });
graph.addVertex('B', { label: 'Checkpoint' });
graph.addVertex('C', { label: 'Junction' });
graph.addVertex('D', { label: 'Station' });
graph.addVertex('E', { label: 'End' });
graph.addEdge('A', 'B', { weight: 2 });
graph.addEdge('B', 'C', { weight: 1 });
graph.addEdge('C', 'D', { weight: 3 });
graph.addEdge('D', 'E', { weight: 1 });
graph.addEdge('A', 'E', { weight: 5 });
// Find shortest path using Dijkstra's algorithm
const shortestPath = graph.findShortestPath('A', 'E');
// Returns { path: ['A', 'B', 'C', 'D', 'E'], distance: 7 }
// Generate minimum spanning tree using Kruskal's algorithm
const mst = graph.minimumSpanningTreeKruskal();
// Returns a new Graph representing the MST
// Alternative: use Prim's algorithm
const mstPrim = graph.minimumSpanningTreePrim();
// Find all-pairs shortest paths using Floyd-Warshall algorithm
const distances = graph.floydWarshall();
// Returns distance matrix between all pairs of vertices
// Community detection using Louvain method
const communities = graph.detectCommunities();
// Returns array of communities (groups of vertices)
// Network analysis
const degreeCentrality = graph.degreeCentrality('C');
const betweennessCentrality = graph.betweennessCentrality();
const closenessCentrality = graph.closenessCentrality('A');
// Detect critical points in the network
const articulationPoints = graph.findArticulationPoints();
const bridges = graph.findBridges();
// For directed acyclic graphs (DAGs)
const sorted = graph.topologicalSort();
```
The graph module integrates seamlessly with other Herta.js features:
```javascript
// Analyze network data using the graph module and statistics
const networkDiameter = graph.diameter();
const avgPathLength = graph.averagePathLength();
const densityValue = graph.density();
// Generate graph visualizations (returns data for plotting)
const layout = graph.forceDirectedLayout();
```
## Features
### Core Mathematical Foundations
- **Pure Mathematics**: Arithmetic, algebra, calculus, and complex analysis
- **Symbolic Mathematics**: Integration, differentiation, and tensor calculus
- **Number Theory**: Prime factorization, modular arithmetic, Diophantine equations, and quadratic residues
- **Abstract Algebra**: Group theory, ring theory, field theory, and Galois theory
### Advanced Mathematics
- **Differential Geometry**: Riemannian metrics, curvature, geodesics, Lie derivatives, and parallel transport
- **Category Theory**: Categories, functors, natural transformations, and universal constructions
- **Algebraic Geometry**: Elliptic curves, toric varieties, blowups, and sheaf cohomology
- **Topology**: Persistent homology, manifold operations, Betti numbers, and simplicial complexes
### Scientific Computing
- **Numerical Methods**: ODEs, PDEs, spectral methods, and stochastic differential equations
- **Linear Algebra**: Large-scale matrix operations and eigenvalue computations
- **Optimization**: Gradient descent, genetic algorithms, simulated annealing, and constrained optimization
- **Signal Processing**: FFT, filter design, convolution, and wavelet transforms
### Physics and Dynamics
- **Quantum Mechanics**: State vectors, density matrices, quantum gates, and measurements
- **Chaos Theory**: Lyapunov exponents, fractals, bifurcation diagrams, and strange attractors
- **Fluid Dynamics**: Reynolds numbers, Navier-Stokes solvers, and flow analysis
- **Relativistic Physics**: Black hole physics, gravitational waves, and spacetime mathematics
### Computer Science
- **Graph Theory**: Network analysis, community detection, flow algorithms, and graph coloring
- **String Algorithms**: Pattern matching (KMP, Boyer-Moore), suffix arrays, and sequence alignment
- **Computer Vision**: Image processing, edge detection, feature matching, and segmentation
- **Machine Learning**: Neural networks, reinforcement learning, and deep learning primitives
### Applied Mathematics
- **Probability & Statistics**: Distributions, hypothesis testing, and Monte Carlo methods
- **Financial Mathematics**: Risk metrics, portfolio optimization, and trading strategies
- **Cryptography**: Encryption, zero-knowledge proofs, and cryptoeconomic models
- **Information Theory**: Entropy, coding theory, and data compression
## Getting Started
### Installation
**Create a new Herta.js project (recommended):**
```bash
# Using npm
npm install -g herta
herta erudition make project MyMathApp
cd my-math-app
# Or directly with npx
npx herta erudition make project MyMathApp
cd my-math-app
```
**Add to an existing project:**
```bash
# Using npm
npm install herta
# Using yarn
yarn add herta
```
### Project Structure
A typical Herta.js project has the following structure:
```
my-math-app/
├── node_modules/
├── src/
│ ├── models/ # Data models
│ ├── services/ # Business logic
│ ├── controllers/ # Route handlers
│ └── utils/ # Helper functions
├── test/ # Test files
├── config.js # Application configuration
├── herta.config.js # Herta.js framework configuration
├── package.json
└── README.md
```
### Quick Start
```javascript
// app.js
const herta = require('herta');
// Initialize the framework
const app = herta.createApplication({
// Configuration options
debug: process.env.NODE_ENV !== 'production',
modules: ['algebra', 'calculus', 'statistics']
});
// Use framework components
const matrix = app.core.matrix.create([[1, 2], [3, 4]]);
const determinant = matrix.determinant();
console.log(`Matrix determinant: ${determinant}`);
// Start the application
app.start();
```
## Herta Erudition CLI
Herta.js comes with a powerful command-line interface called "Erudition" that helps you scaffold, analyze, test, document, and understand the framework.
### CLI Usage
Once Herta.js is installed globally (see Installation section above), the CLI is automatically available. Or you can use it directly with npx from within your project:
```bash
npx herta erudition <command>
```
### Available Commands
#### `make` - Generate Code Components
Create boilerplate code for various components:
```bash
herta erudition make module MyModule # Create a new module
herta erudition make controller DataController # Create a new controller
herta erudition make service AnalysisService # Create a new service
herta erudition make test QuantumModule # Create a new test file
herta erudition make api UserManagement # Create a complete REST API
herta erudition make rest-controller Products # Create a REST controller
herta erudition make graphql BookSchema # Create GraphQL schema and resolver
```
The `make` command generates proper file structure and boilerplate code with appropriate naming conventions (PascalCase, camelCase, snake_case) depending on the component type.
##### API Generator Options
The API generator creates fully-functional RESTful endpoints:
```bash
# Create a complete User Management API with all required files
herta erudition make api UserManagement --with-auth # Include authentication
herta erudition make api DataAnalytics --with-validation # Include validation
herta erudition make api SensorData --with-docs # Include Swagger docs
```
##### REST Controller Options
```bash
# Generate specialized REST controllers
herta erudition make rest-controller Products --crud # Basic CRUD operations
herta erudition make rest-controller Orders --with-relations # Include related resources
herta erudition make rest-controller Analytics --read-only # Read-only endpoints
```
##### GraphQL Generator Options
```bash
# Generate GraphQL components
herta erudition make graphql BookSchema --with-resolvers # Include resolvers
herta erudition make graphql UserSchema --with-mutations # Include mutations
herta erudition make graphql OrderSchema --with-directives # Include custom directives
```
#### `analyze` - Analyze Code Quality and Patterns
Perform static analysis on your code:
```bash
herta erudition analyze src/advanced/ # Default analysis (stats)
herta erudition analyze --complexity src/core/ # Analyze code complexity
herta erudition analyze --dependencies src/utils/ # Analyze module dependencies
herta erudition analyze --stats src/algorithms/ # Analyze code statistics
```
The `analyze` command examines your code and provides insights on complexity metrics, dependency graphs, and code statistics to help improve code quality.
#### `doc` - Generate Documentation
Automatically generate documentation from JSDoc comments:
```bash
herta erudition doc matrix # Generate docs for a specific component
herta erudition doc --all # Generate docs for all components
herta erudition doc --format html # Generate in HTML format (default is markdown)
herta erudition doc --output custom-docs/ # Specify output directory
```
The `doc` command extracts JSDoc comments from your code and generates comprehensive documentation in markdown or HTML format.
#### `test` - Run Tests with Detailed Reporting
Execute tests with comprehensive reports:
```bash
herta erudition test core/matrix # Run tests matching a specific pattern
herta erudition test --unit # Run only unit tests
herta erudition test --integration # Run only integration tests
herta erudition test --coverage # Generate test coverage report
herta erudition test --verbose # Show detailed test output
```
The `test` command runs tests and provides detailed summaries of test results, including pass/fail counts, execution time, and coverage metrics.
#### `explain` - Get Plain English Explanations
Receive explanations of framework concepts in plain language:
```bash
herta erudition explain config # Explain configuration options
herta erudition explain algorithms # Explain general algorithms concepts
herta erudition explain eigenvalues # Explain mathematical concepts
herta erudition explain --detailed quantum # Get more detailed explanation
```
The `explain` command provides clear, plain-English explanations of Herta.js concepts, configuration options, and advanced mathematical and scientific topics. It's especially useful for understanding complex modules like Quantum Mechanics, Chaos Theory, and Number Theory.
#### `api` - Create and Manage APIs
Build and manage APIs powered by Herta.js:
```bash
herta erudition api create --name math-api --type rest # Create a new REST API
herta erudition api add-endpoint calculus --module calculus # Add module endpoint
herta erudition api generate-docs --format openapi # Generate API docs
herta erudition api test --endpoint graph-analysis # Test an endpoint
herta erudition api deploy --provider aws # Deploy the API
```
The `api` command helps you build APIs that leverage Herta.js's mathematical capabilities, enabling powerful computation services.
#### `web` - Web Development Tools
Create web applications and components with Herta.js:
```bash
herta erudition web create-app --name matrix-explorer # Create a web app
herta erudition web add-component matrix-visualizer # Add a component
herta erudition web build --optimize # Build for production
herta erudition web serve --port 3000 # Start dev server
herta erudition web generate-ssr --framework next # Add SSR support
```
The `web` command facilitates creating rich web applications that utilize Herta.js's mathematical modules for visualization, computation, and analysis.
### Additional CLI Functions
#### Global Options
All Erudition commands support these common options:
```bash
--help, -h # Display command-specific help
--version, -v # Display Erudition version information
--quiet, -q # Reduce output verbosity
--json # Output results in JSON format
--config <file> # Use custom configuration file
```
#### Sub-command Details
##### Make Command Templates
The `make` command supports various templates for different component types:
```bash
herta erudition make module --functional MyModule # Create a functional module
herta erudition make module --class MyModule # Create a class-based module
herta erudition make test --unit MyTest # Create a unit test
herta erudition make test --integration MyTest # Create an integration test
herta erudition make test --performance MyTest # Create a performance test
```
##### Analyze Command Options
The `analyze` command provides specialized analysis modes:
```bash
herta erudition analyze --complexity-threshold 10 # Flag methods over threshold
herta erudition analyze --visualize # Generate visual reports
herta erudition analyze --trends # Compare to historical data
herta erudition analyze --unused # Find unused code
```
##### Documentation Format Options
The `doc` command supports different output formats and templates:
```bash
herta erudition doc --format html --theme dark # Dark themed HTML docs
herta erudition doc --format markdown --toc # Markdown with table of contents
herta erudition doc --template custom-template.ejs # Use custom template
herta erudition doc --diagrams # Include UML diagrams
```
##### Test Reporting Options
The `test` command has additional reporting capabilities:
```bash
herta erudition test --reporters mocha,junit # Multiple report formats
herta erudition test --watch # Watch mode for auto-reruns
herta erudition test --parallel 4 # Run tests in parallel
herta erudition test --snapshot # Run snapshot tests
herta erudition test --perf # Include performance benchmarks
```
##### Explain Command Topics
The `explain` command covers numerous specialized topics:
```bash
herta erudition explain architecture # System architecture overview
herta erudition explain modules # Module system details
herta erudition explain patterns # Design patterns used
herta erudition explain best-practices # Coding best practices
herta erudition explain api-stability # API stability guarantees
```
### Module-Specific Commands
Erudition includes specialized commands for working with Herta.js's advanced mathematical modules:
#### Quantum Mechanics Helper
```bash
# Visualize quantum states and gates
herta erudition quantum visualize --state "[0.7071, 0.7071]" --output quantum-state.png
herta erudition quantum circuit --gates "H,X,CNOT" --qubits 2 --output circuit.svg
# Generate common quantum circuits
herta erudition quantum generate --type bell-state --qubits 2
herta erudition quantum generate --type qft --qubits 4
# Analyze quantum properties
herta erudition quantum analyze-entanglement --state "[0.7071, 0, 0, 0.7071]"
```
#### Graph Theory Utilities
```bash
# Generate and analyze graphs
herta erudition graph generate --type erdos-renyi --nodes 20 --probability 0.3
herta erudition graph generate --type scale-free --nodes 50 --output graph.json
# Analyze graph properties
herta erudition graph analyze --file graph.json --metrics "centrality,connectivity,communities"
herta erudition graph visualize --file graph.json --layout force-directed --output graph.svg
```
#### Optimization Playground
```bash
# Test optimization algorithms on standard problems
herta erudition optimize --algorithm gradient-descent --problem rosenbrock
herta erudition optimize --algorithm genetic --problem traveling-salesman --cities 15
# Benchmark optimization performance
herta erudition optimize benchmark --algorithms "gradient-descent,genetic,simulated-annealing" --problem sphere
```
#### Data Science Toolkit
```bash
# Statistical analysis and probability tools
herta erudition stats analyze --data data.csv --tests "normality,correlation,t-test"
herta erudition stats generate --distribution normal --params "0,1" --samples 1000 --output samples.csv
# Monte Carlo simulations
herta erudition stats monte-carlo --simulation coin-toss --trials 10000 --probability 0.5
```
#### Number Theory Explorer
```bash
# Prime number utilities
herta erudition number-theory primes --range "1,1000" --output primes.json
herta erudition number-theory factorize --number 12345678 --algorithm "pollard-rho"
# Explore number properties
herta erudition number-theory properties --number 42 --checks "prime,perfect,abundant,deficient"
# Modular arithmetic operations
herta erudition number-theory solve-congruence --equation "3x ≡ 5 (mod 7)"
herta erudition number-theory chinese-remainder --remainders "2,3,2" --moduli "3,5,7"
```
#### Fluid Dynamics Simulator
```bash
# Calculate fluid properties
herta erudition fluid reynolds --velocity 2 --diameter 0.05 --fluid water
herta erudition fluid pressure-drop --length 10 --diameter 0.05 --velocity 2 --roughness 0.0001
# Run fluid simulations
herta erudition fluid simulate --model "1d-advection" --domain-length 10 --nodes 100 --time-steps 1000
herta erudition fluid simulate --model "2d-incompressible" --resolution "100x100" --time 10 --output flow.mp4
# Visualize fluid results
herta erudition fluid visualize --data simulation.json --type "velocity-field" --output velocity.png
herta erudition fluid visualize --data simulation.json --type "pressure-contour" --output pressure.png
```
#### Computer Vision Toolkit
```bash
# Image processing operations
herta erudition vision process --input image.jpg --operations "grayscale,gaussian-blur:5,canny:30:100" --output processed.jpg
herta erudition vision histogram --input image.jpg --channel rgb --equalize --output histogram.png
# Feature detection and analysis
herta erudition vision detect-features --input image.jpg --algorithm "fast" --threshold 20 --output features.json
herta erudition vision match-features --image1 scene.jpg --image2 object.jpg --output matches.jpg
# Advanced computer vision operations
herta erudition vision segment --input image.jpg --algorithm "kmeans" --clusters 5 --output segmented.jpg
herta erudition vision detect-objects --input image.jpg --model "herta-detector" --confidence 0.7 --output detected.jpg
```
#### String Algorithms Workshop
```bash
# Pattern matching
herta erudition strings search --text file.txt --pattern "important text" --algorithm "kmp" --output matches.json
herta erudition strings multi-pattern --text file.txt --patterns patterns.txt --algorithm "aho-corasick" --output results.json
# String similarity and distance
herta erudition strings compare --string1 "kitten" --string2 "sitting" --metrics "levenshtein,lcs,similarity"
herta erudition strings cluster --input strings.txt --method "edit-distance" --threshold 3 --output clusters.json
# Suffix structures and compression
herta erudition strings build-suffix-array --text "banana" --output suffix-array.json
herta erudition strings compress --input file.txt --method "run-length" --output compressed.txt
```
#### Chaos Theory Explorer
```bash
# Generate fractal visualizations
herta erudition chaos fractal --type "mandelbrot" --bounds "-2:1:-1:1" --resolution "1000x1000" --output mandelbrot.png
herta erudition chaos julia --c "-0.7:0.27" --bounds "-2:2:-2:2" --resolution "1000x1000" --output julia.png
# Simulate chaotic systems
herta erudition chaos simulate --system "lorenz" --params "10,28,8/3" --duration 100 --output lorenz.json
herta erudition chaos simulate --system "logistic-map" --param 3.9 --iterations 1000 --output bifurcation.json
# Analyze chaotic properties
herta erudition chaos lyapunov --data timeseries.json --dimension 3 --delay 2 --output lyapunov.json
herta erudition chaos recurrence-plot --data timeseries.json --threshold 0.1 --output recurrence.png
```
#### Advanced Mathematics Explorer
```bash
# Differential Geometry operations
herta erudition differential-geometry manifold --dimension 2 --curvature constant --output sphere.json
herta erudition differential-geometry geodesic --manifold sphere.json --start "0,0" --end "pi/2,pi/2" --output geodesic.json
# Category Theory visualization
herta erudition category diagram --objects "A,B,C,D" --morphisms "f:A->B,g:B->C,h:A->D,i:D->C" --output diagram.svg
herta erudition category functor --source diagram1.json --target diagram2.json --output functor.json
# Complex Analysis tools
herta erudition complex-analysis contour-plot --function "z^2" --region "-2:2:-2:2" --resolution 500 --output contour.png
herta erudition complex-analysis laurent-series --function "1/(z^2-1)" --point 0 --terms 10
```
#### Cross-Domain Integration Tools
```bash
# Machine Learning with Herta modules
herta erudition ml train --algorithm "neural-network" --data dataset.csv --features "x,y,z" --target "output" --model-save model.json
herta erudition ml optimize-hyperparams --algorithm "svm" --data dataset.csv --param-grid params.json --output best-params.json
# Scientific computing pipelines
herta erudition pipeline create --name "data-analysis" --steps "load:data.csv,clean,normalize,analyze:correlation,visualize:heatmap" --output pipeline.json
herta erudition pipeline run --file pipeline.json --params params.json --output results/
# Interactive explorations
herta erudition explore --module quantum --interactive # Opens an interactive jupyter-like notebook
herta erudition explore --module graph --dataset social-network.json --interactive
```
#### Developer Utilities
```bash
# Code quality and maintenance
herta erudition dev audit-dependencies --security --outdated
herta erudition dev lint --fix --style standard
# Performance profiling
herta erudition dev profile --function "matrix.multiply" --size "1000x1000" --iterations 10 --output profile.json
herta erudition dev benchmark --suite "matrix-operations" --compare-with v1.0.0 --output benchmark.html
# Versioning and publishing
herta erudition dev prepare-release --bump minor --changelog --tag
herta erudition dev publish --dry-run # Verify everything before actual publish
```
#### API & Web Development
```bash
# API integration with mathematical modules
herta erudition api create --type rest --name mathematical-api
herta erudition api add-endpoint optimization --methods "POST,GET" --module optimization
herta erudition api add-endpoint quantum --methods "POST" --module quantum
herta erudition api add-endpoint graph-analysis --methods "POST,GET" --module graph
# Generate client libraries for mathematical APIs
herta erudition api client-gen --language javascript --output ./clients/js
herta erudition api client-gen --language python --output ./clients/python
herta erudition api client-gen --language typescript --with-types --output ./clients/ts
# Web component generation for visualization
herta erudition web component graph-visualizer --module graph --output ./components
herta erudition web component matrix-editor --module matrix --output ./components
herta erudition web component fractal-explorer --module chaos --output ./components
# Create interactive web dashboards for mathematical modules
herta erudition web dashboard optimization --algorithms "gradient-descent,genetic" --output ./dashboards
herta erudition web dashboard quantum --features "state-visualization,gate-simulation" --output ./dashboards
herta erudition web dashboard statistics --features "distribution-explorer,regression" --output ./dashboards
```
### Extending Erudition
You can create custom Erudition plugins by adding files to the `commands/erudition/plugins` directory:
```javascript
// commands/erudition/plugins/myPlugin.js
module.exports = function(args) {
// Plugin implementation
console.log('My custom plugin is running!');
};
```
Then use your plugin with:
```bash
herta erudition myPlugin [args]
```
## Basic Usage
```javascript
const herta = require('herta');
// Basic arithmetic and constants
console.log(herta.round(herta.e, 3)); // 2.718
console.log(herta.atan2(3, -3) / herta.pi); // 0.75
console.log(herta.log(10000, 10)); // 4
console.log(herta.sqrt(-4).toString()); // 2i
// Matrix operations
const matrix = [[-1, 2], [3, 1]];
console.log(herta.pow(matrix, 2)); // [[7, 0], [0, 7]]
console.log(herta.det(matrix)); // -7
// Expression evaluation
console.log(herta.evaluate('1.2 * (2 + 4.5)')); // 7.8
console.log(herta.evaluate('12.7 cm to inch')); // 5 inch
console.log(herta.evaluate('sin(45 deg) ^ 2')); // 0.5
console.log(herta.evaluate('9 / 3 + 2i')); // 3 + 2i
// Chaining operations
const result = herta.chain(3)
.add(4)
.multiply(2)
.done(); // 14
console.log(result);
```
## Advanced Features
### Symbolic Integration
```javascript
// Advanced symbolic integration
const integral = herta.integrate('sin(x^2)', 'x');
console.log(integral); // Complex symbolic result
// Definite integration
const definiteIntegral = herta.integrate('sin(x^2)', 'x', 0, 1);
console.log(definiteIntegral); // Numerical approximation
```
### Differential Equations
```javascript
// Solving differential equations
const solution = herta.solveDifferentialEquation('dy/dx = y^2 + x', 'y', 'x');
console.log(solution); // Symbolic solution
// Numerical solution with initial conditions
const numericalSolution = herta.solveODE('y\'=y^2+x', 'y', 'x', 0, 1, [0, 5]);
console.log(numericalSolution); // Array of solution points
```
### Large-Scale Linear Algebra
```javascript
// Create a large sparse matrix
const sparseMatrix = herta.createSparseMatrix(1000, 1000);
// Efficient eigenvalue computation
const eigenvalues = herta.eigenvalues(sparseMatrix);
console.log(eigenvalues.length); // 1000
```
### Tensor Calculus
```javascript
// Create tensors for General Relativity calculations
const metric = herta.createMetricTensor(4, 4); // 4D spacetime metric
// Compute Christoffel symbols
const christoffel = herta.christoffelSymbols(metric);
console.log(christoffel); // 3D tensor
// Compute Riemann curvature tensor
const riemann = herta.riemannTensor(metric);
console.log(riemann); // 4D tensor
```
### Automatic Differentiation
```javascript
// Define a function for automatic differentiation
const f = herta.autodiff.createFunction('x^2 + sin(y) + z*x', ['x', 'y', 'z']);
// Compute gradient at a point
const gradient = f.gradient([2, Math.PI, 3]);
console.log(gradient); // [7, 0, 2]
// Compute Hessian matrix
const hessian = f.hessian([2, Math.PI, 3]);
console.log(hessian); // [[2, 0, 1], [0, -1, 0], [1, 0, 0]]
```
## Module Examples
### Advanced Algebra
```javascript
// Working with group theory
const elements = [0, 1, 2, 3, 4]; // Z5 elements
const operation = (a, b) => (a + b) % 5; // Addition modulo 5
// Create a cyclic group
const Z5 = herta.advancedAlgebra.createCyclicGroup(5);
// Group operations
console.log(Z5.apply(2, 3)); // 0 (2+3 mod 5)
console.log(Z5.inverse(3)); // 2 (since 3+2=0 mod 5)
console.log(Z5.orderOf(2)); // 5
// Create a modular ring
const modRing = herta.advancedAlgebra.createModularRing(7);
console.log(modRing.power(3, -1)); // 5 (modular inverse of 3 mod 7)
// Polynomial operations
const F = herta.advancedAlgebra.createModularRing(5); // Field Z/5Z
const poly1 = herta.advancedAlgebra.createPolynomial([1, 0, 1], F); // x^2 + 1
const poly2 = herta.advancedAlgebra.createPolynomial([2, 1], F); // x + 2
const product = poly1.multiply(poly2); // (x^2 + 1)(x + 2) = x^3 + 2x^2 + x + 2
```
### Reinforcement Learning
```javascript
// Create a GridWorld environment
const gridWorld = herta.reinforcementLearning.GridWorld(5, 5, {
start: { x: 0, y: 0 },
terminals: [
{ x: 4, y: 4, reward: 1 }, // Goal state with positive reward
{ x: 4, y: 0, reward: -1 }, // Negative reward state
],
obstacles: [
{ x: 2, y: 1 }, { x: 2, y: 2 }, { x: 2, y: 3 }, // Wall
],
defaultReward: -0.04, // Small penalty for each step
});
// Create a Q-learning agent
const agent = herta.reinforcementLearning.QLearning(gridWorld, {
learningRate: 0.1,
discountFactor: 0.9,
epsilon: 0.2,
epsilonDecay: 0.995,
});
// Train the agent
const trainingResults = agent.train(1000); // Train for 1000 episodes
// Get the learned policy
const policy = agent.getPolicy();
console.log(policy); // Maps states to optimal actions
// Multi-armed bandit example
const banditRewards = (armIndex) => {
const means = [0.3, 0.5, 0.7, 0.9]; // Mean rewards for 4 arms
return means[armIndex] + 0.1 * (Math.random() - 0.5); // Add noise
};
// Create a UCB bandit solver
const bandit = herta.reinforcementLearning.ucbBandit(banditRewards, 4);
const results = bandit.run(1000); // Run for 1000 steps
```
### Text Analysis
```javascript
// Basic text tokenization and processing
const text = "Natural language processing is fascinating. It has many applications in AI."
const tokens = herta.textAnalysis.tokenize(text);
console.log(tokens); // ['Natural', 'language', 'processing', ...]
// Sentence tokenization
const sentences = herta.textAnalysis.tokenize(text, 'sentence');
console.log(sentences); // ['Natural language processing is fascinating.', ...]
// TF-IDF calculation
const documents = [
"This is a document about dogs.",
"This document discusses cats and their behavior.",
"Dogs and cats are popular pets."
];
const idf = herta.textAnalysis.inverseDocumentFrequency(documents);
const tfidf = herta.textAnalysis.tfidf(documents[0], idf);
console.log(tfidf); // { 'document': 0.1053..., 'dogs': 0.4054... }
// Sentiment analysis
const sentiment = herta.textAnalysis.analyzeSentiment("I love this product, it's amazing!");
console.log(sentiment); // { positive: 0.2, negative: 0, score: 0.2, magnitude: 0.2 }
// Create a text classifier
const classifier = herta.textAnalysis.createNaiveBayesClassifier();
// Train the classifier
classifier.train("This product is excellent", "positive");
classifier.train("I'm very happy with my purchase", "positive");
classifier.train("This doesn't work at all", "negative");
classifier.train("Poor quality and expensive", "negative");
// Classify new text
const result = classifier.classify("I'm happy with this product");
console.log(result.category); // 'positive'
```
### Cryptoeconomics
```javascript
// Create a tokenomics model for a cryptocurrency
const tokenModel = herta.cryptoeconomics.createTokenomicsModel({
initialSupply: 100000000, // 100 million tokens
inflationRate: 0.02, // 2% annual inflation
stakingRewardRate: 0.05, // 5% annual staking rewards
burnRate: 0.001, // 0.1% tokens burned per year
distributionRatios: {
team: 0.15,
investors: 0.15,
community: 0.6,
reserves: 0.1
}
});
// Project supply after 10 years
const supplyProjection = tokenModel.projectSupply(10);
console.log(supplyProjection.finalSupply); // Future token supply
// Calculate staking rewards
const stakingRewards = tokenModel.calculateStakingRewards(1000000, 5);
console.log(stakingRewards); // Returns staking rewards over 5 years
// Create a bonding curve pricing model
const curve = herta.cryptoeconomics.createBondingCurve({
curveType: 'linear',
slope: 0.1,
initialPrice: 1
});
// Calculate token price impact
const impact = curve.simulatePriceImpact(1000, true);
console.log(impact); // Shows price before and after 1000-token purchase
```
### Zero Knowledge Proofs
```javascript
// Create a range proof system
const rangeProver = herta.zeroKnowledgeProofs.rangeProof();
// Generate a proof that a value is within range [0, 1000]
const proof = rangeProver.prove(750, 1000);
// Verify the proof
const isValid = rangeProver.verify(proof, 1000);
console.log(isValid); // true - confirms value is in range without revealing it
// Set membership proof
const allowlist = ["address1", "address2", "address3"];
const membershipProof = herta.zeroKnowledgeProofs.setMembership(allowlist);
// Prove membership without revealing which element
const addressProof = membershipProof.proveInSet("address2");
// Verify membership
const isMember = membershipProof.verifyInSet(addressProof);
console.log(isMember); // true - confirms address is in the allowlist
```
### Language Model Math
```javascript
// Calculate self-attention scores in a transformer model
const queries = [[1, 0, 1], [0, 1, 0]]; // Query vectors
const keys = [[1, 1, 0], [0, 1, 1]]; // Key vectors
// Calculate attention weights
const attentionWeights = herta.languageModelMath.selfAttention(queries, keys);
console.log(attentionWeights); // Attention distribution
// Apply attention to values
const values = [[1, 2], [3, 4]]; // Value vectors
const attentionOutput = herta.languageModelMath.applyAttention(attentionWeights, values);
// Apply position encoding to embeddings
const embeddings = [[1, 2, 3, 4], [5, 6, 7, 8]];
const positionEncoded = herta.languageModelMath.positionEncoding(embeddings);
// Sample from a distribution using nucleus sampling (top-p)
const logits = [2.5, 1.2, 0.8, 0.3, -0.5]; // Raw model outputs
const sampledToken = herta.languageModelMath.nucleusSampling(logits, 0.9);
console.log(sampledToken); // Token ID selected by sampling
```
### Neural Networks
```javascript
// Create a feedforward neural network
const model = herta.neuralNetworks.feedForward(
[784, 128, 64, 10], // Layer sizes (MNIST classification example)
{ activation: 'relu', outputActivation: 'softmax' }
);
// Forward pass
const input = [/* flattened image data */];
const prediction = model.forward([input]);
// Create a convolutional layer
const convLayer = herta.neuralNetworks.convLayer2d({
inputChannels: 1,
outputChannels: 16,
kernelSize: 3,
stride: 1,
padding: 1,
activation: 'relu'
});
// Create an LSTM layer
const lstm = herta.neuralNetworks.lstmLayer(100, 64, { returnSequences: true });
// Process a sequence through the LSTM
const sequence = [/* sequence of embedding vectors */];
const { output, hidden, cell } = lstm.forward([sequence]);
```
### Relativistic Astrophysics
```javascript
// Calculate the Schwarzschild radius of a black hole
const solarMass = 1.989e30; // kg
const blackHoleMass = 10 * solarMass; // 10 solar masses
const radius = herta.relativisticAstrophysics.schwarzschildRadius(blackHoleMass);
console.log(radius); // ~29.5 km
// Calculate gravitational time dilation
const distance = radius * 3; // 3 times the Schwarzschild radius
const timeDilation = herta.relativisticAstrophysics.gravitationalTimeDilation(
blackHoleMass,
distance
);
console.log(timeDilation); // Time runs slower by this factor
// Calculate properties of a binary black hole merger
const mass1 = 30 * solarMass;
const mass2 = 25 * solarMass;
const separation = 1000000; // 1000 km
// Gravitational wave frequency
const freq = herta.relativisticAstrophysics.gravitationalWaveFrequency(
mass1,
mass2,
separation
);
// Time until merger
const timeToMerge = herta.relativisticAstrophysics.timeToMerger(
mass1,
mass2,
separation
);
console.log(timeToMerge); // Time in seconds until the black holes merge
```
### Technical Analysis
```javascript
// Calculate Simple Moving Average
const prices = [10.5, 11.2, 10.8, 11.5, 11.7, 12.1, 12.5, 12.0, 11.8, 12.2];
const sma = herta.technicalAnalysis.sma(prices, 5);
console.log(sma); // [11.14, 11.46, 11.72, 11.96, 12.12, 12.1]
// Calculate Relative Strength Index
const priceHistory = [45.34, 45.67, 46.12, 46.82, 46.45, 46.89, 47.23, 47.56, 47.32, 47.65, 48.12, 48.45, 48.23, 47.98];
const rsi = herta.technicalAnalysis.rsi(priceHistory, 14);
console.log(rsi); // Returns RSI values
// Calculate Bollinger Bands
const priceSeries = [26.10, 25.78, 26.01, 26.35, 26.57, 26.42, 26.35, 26.70, 26.89, 26.95, 27.12, 27.42, 27.67, 27.85];
const { upperBand, middleBand, lowerBand } = herta.technicalAnalysis.bollingerBands(priceSeries, 10, 2);
console.log('Upper Band:', upperBand);
console.log('Middle Band:', middleBand);
console.log('Lower Band:', lowerBand);
// Detect support and resistance with Pivot Points
const pivots = herta.technicalAnalysis.pivotPoints(27.85, 26.35, 27.45);
console.log('Pivot:', pivots.pivot);
console.log('Resistance 1:', pivots.resistance.r1);
console.log('Support 1:', pivots.support.s1);
// Calculate MACD
const { macdLine, signalLine, histogram } = herta.technicalAnalysis.macd(priceSeries);
console.log('MACD line:', macdLine);
console.log('Signal line:', signalLine);
console.log('Histogram:', histogram);
```
### Trading Strategies
```javascript
// Calculate optimal position size using Kelly Criterion
const winRate = 0.55; // 55% of trades are profitable
const winLossRatio = 1.5; // Average win is 1.5x the average loss
const capital = 10000; // $10,000 trading capital
const positionSize = herta.tradingStrategies.kellyPositionSize(winRate, winLossRatio, capital);
console.log('Optimal position size:', positionSize); // $1,666.67
// Calculate stop loss and take profit based on volatility
const priceData = [
{ high: 150.5, low: 149.2, close: 150.1 },
{ high: 151.2, low: 149.8, close: 151.0 },
// ... more price history
];
const entryPrice = 151.5;
const levels = herta.tradingStrategies.volatilityBasedLevels(entryPrice, priceData, 2, 3);
console.log('Entry price:', levels.entryPrice);
console.log('Stop loss:', levels.stopLoss);
console.log('Take profit:', levels.takeProfit);
// Implement a trend following strategy
const btcPrices = [
// Daily price data for Bitcoin
];
const signals = herta.tradingStrategies.trendFollowing(btcPrices, {
shortPeriod: 10,
longPeriod: 30,
stopLossPercent: 0.05
});
console.log('Strategy signals:', signals);
// Evaluate trading performance
const trades = [
{ entryPrice: 10000, exitPrice: 10500, type: 'long', size: 0.1 },
{ entryPrice: 10500, exitPrice: 10300, type: 'short', size: 0.1 },
{ entryPrice: 10300, exitPrice: 11000, type: 'long', size: 0.15 }
];
const performance = herta.tradingStrategies.evaluatePerformance(trades, 5000);
console.log('Total return:', performance.totalReturns);
console.log('Sharpe ratio:', performance.sharpeRatio);
console.log('Win rate:', performance.winRate);
```
### Risk Management
```javascript
// Calculate Value at Risk (VaR) using historical simulation
const portfolioValue = 1000000; // $1 million portfolio
const returns = [-0.02, -0.015, -0.01, -0.005, 0, 0.005, 0.01, 0.015, 0.02]; // Historical returns
const var95 = herta.riskManagement.historicalVaR(returns, 0.95, portfolioValue);
console.log('95% VaR:', var95); // Amount you could lose with 95% confidence
// Calculate parametric Value at Risk
const mean = 0.001; // Mean daily return
const stdDev = 0.015; // Standard deviation of returns
const parametricVaR = herta.riskManagement.parametricVaR(mean, stdDev, 0.99, portfolioValue);
console.log('99% Parametric VaR:', parametricVaR);
// Calculate portfolio volatility
const weights = [0.4, 0.3, 0.2, 0.1]; // Asset allocation weights
const covarianceMatrix = [
[0.04, 0.02, 0.01, 0.01],
[0.02, 0.09, 0.01, 0.02],
[0.01, 0.01, 0.16, 0.01],
[0.01, 0.02, 0.01, 0.04]
];
const volatility = herta.riskManagement.portfolioVolatility(weights, covarianceMatrix);
console.log('Portfolio Volatility:', volatility);
// Calculate Sharpe Ratio
const expectedReturn = 0.12; // 12% annual expected return
const annualVolatility = 0.18; // 18% annual volatility
const riskFreeRate = 0.03; // 3% risk-free rate
const sharpeRatio = herta.riskManagement.sharpeRatio(expectedReturn, annualVolatility, riskFreeRate);
console.log('Sharpe Ratio:', sharpeRatio);
// Perform a stress test
const assetValues = [400000, 300000, 200000, 100000]; // Values of assets in portfolio
const stressShocks = [0.25, 0.15, 0.35, 0.10]; // Stress scenario percentage losses
const stressTest = herta.riskManagement.stressTest(assetValues, stressShocks);
console.log('Stressed Portfolio Value:', stressTest.stressedValue);
console.log('Total Impact:', stressTest.impactAmount);
console.log('Impact Percentage:', stressTest.impactPercent);
```
### Tabular Analysis
```javascript
// Analyze a dataset of sales transactions
const salesData = [
{ date: '2023-01-01', product: 'A', region: 'North', sales: 1200, units: 5, returns: 0 },
{ date: '2023-01-01', product: 'B', region: 'North', sales: 900, units: 3, returns: 1 },
{ date: '2023-01-02', product: 'A', region: 'South', sales: 1500, units: 6, returns: 0 },
{ date: '2023-01-02', product: 'B', region: 'East', sales: 1100, units: 4, returns: 0 },
{ date: '2023-01-03', product: 'C', region: 'West', sales: 1800, units: 7, returns: 2 },
{ date: '2023-01-03', product: 'A', region: 'East', sales: 2000, units: 8, returns: 1 },
{ date: '2023-01-04', product: 'B', region: 'West', sales: 1300, units: 5, returns: 0 },
{ date: '2023-01-04', product: 'C', region: 'North', sales: 950, units: 3, returns: 0 },
];
// Generate summary statistics for numeric columns
const summary = herta.tabularAnalysis.summarize(salesData);
console.log('Sales Summary:', summary.sales);
console.log('Units Summary:', summary.units);
// Calculate correlation matrix
const { correlationMatrix } = herta.tabularAnalysis.correlationMatrix(salesData);
console.log('Correlation between sales and units:', correlationMatrix.sales.units);
// Detect outliers
const outliers = herta.tabularAnalysis.detectOutliers(salesData, ['sales', 'units']);
console.log('Sales outliers:', outliers.sales);
// Group data by product and calculate aggregates
const salesByProduct = herta.tabularAnalysis.groupBy(salesData, 'product', {
sales: 'sum',
units: 'sum',
avgSale: (values) => values.reduce((sum, val) => sum + val, 0) / values.length
});
console.log('Sales by Product:', salesByProduct);
// Normalize the sales data
const { data: normalizedData } = herta.tabularAnalysis.normalize(salesData, ['sales', 'units']);
console.log('Normalized Data:', normalizedData);
// One-hot encode categorical variables
const encodedData = herta.tabularAnalysis.oneHotEncode(salesData, ['region', 'product']);
console.log('Encoded Data (first row):', encodedData[0]);
```
## Herta Erudition CLI
Herta.js comes with a powerful CLI tool called "Erudition" that helps you scaffold, analyze, test, document, and understand the framework.
### Installation
The CLI is automatically available when you install herta:
```bash
npm install herta
```
After installation, you can access the CLI globally:
```bash
herta erudition <command> [options]
```
### Available Commands
#### Scaffolding with `make`
```bash
# Create a new module
herta erudition make module QuantumPhysics
# Create a new controller
herta erudition make controller UserController
# Create a new service
herta erudition make service DataService
# Create a new test
herta erudition make test Vector
```
#### Analyzing Code with `analyze`
```bash
# General analysis
herta erudition analyze
# Specific analysis types
herta erudition analyze --complexity
herta erudition analyze --dependencies
```
#### Generating Documentation with `doc`
```bash
# Document a specific module
herta erudition doc matrix
# Generate all documentation
herta erudition doc --all
```
#### Running Tests with `test`
```bash
# Run all tests
herta erudition test
# Run with coverage
herta erudition test --coverage
# Run specific tests
herta erudition test matrix.test.js
```
#### Understanding the Framework with `explain`
```bash
# Explain configuration options
herta erudition explain config
# Explain algorithms used
herta erudition explain algorithms
# Explain specific modules
herta erudition explain module:neuralNetworks
```
## Project Structure
```
herta/
├── bin/ # CLI tools
├── commands/ # CLI command implementations
│ └── erudition/ # Erudition CLI commands
├── src/
│ ├── core/ # Core mathematical functionality
│ ├── advanced/