slangmath 1.0.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/logo.png ADDED
Binary file
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "slangmath",
3
- "version": "1.0.0",
4
- "description": "🎯 **Advanced Symbolic Mathematics with Comprehensive LaTeX Conversion & Extended Function Support**",
5
- "main": "slang-advanced.js",
6
- "directories": {
7
- "test": "tests"
8
- },
9
- "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/SENODROOM/SLaNg.git"
15
- },
16
- "keywords": [],
17
- "author": "",
18
- "license": "ISC",
19
- "bugs": {
20
- "url": "https://github.com/SENODROOM/SLaNg/issues"
21
- },
22
- "homepage": "https://github.com/SENODROOM/SLaNg#readme"
23
- }
1
+ {
2
+ "name": "slangmath",
3
+ "version": "1.0.3",
4
+ "description": "🎯 **Advanced Symbolic Mathematics with Comprehensive LaTeX Conversion & Extended Function Support**",
5
+ "main": "slang-math.js",
6
+ "directories": {
7
+ "test": "tests"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/SENODROOM/SLaNg.git"
15
+ },
16
+ "keywords": [],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "bugs": {
20
+ "url": "https://github.com/SENODROOM/SLaNg/issues"
21
+ },
22
+ "homepage": "https://github.com/SENODROOM/SLaNg#readme"
23
+ }
package/slang-extended.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * Extends the converter with support for trigonometric, logarithmic, and other functions
4
4
  */
5
5
 
6
- import { createTerm, createFraction } from './slang-basic.js';
6
+ import { createTerm, createFraction, multiplyTerms, differentiatePolynomial } from './slang-basic.js';
7
7
  import { slangToLatex, latexToSlang } from './slang-convertor.js';
8
8
 
9
9
  // ============================================================================
@@ -432,6 +432,423 @@ export function getFunctionLatex(name) {
432
432
  return info ? info.latex : null;
433
433
  }
434
434
 
435
+ // ============================================================================
436
+ // ADVANCED MATHEMATICAL FUNCTIONS
437
+ // ============================================================================
438
+
439
+ /**
440
+ * Calculate gradient of a multivariable function
441
+ * @param {Object} func - Function object
442
+ * @param {Array} variables - Array of variable names ['x', 'y', 'z', ...]
443
+ * @returns {Object} Gradient object with partial derivatives
444
+ */
445
+ export function gradient(func, variables) {
446
+ const grad = {};
447
+
448
+ for (const variable of variables) {
449
+ if (func.type === 'function') {
450
+ grad[variable] = differentiateFunction(func, variable);
451
+ } else {
452
+ // Handle polynomial differentiation
453
+ grad[variable] = differentiatePolynomial(func, variable);
454
+ }
455
+ }
456
+
457
+ return grad;
458
+ }
459
+
460
+ /**
461
+ * Calculate Hessian matrix (second partial derivatives)
462
+ * @param {Object} func - Function object
463
+ * @param {Array} variables - Array of variable names
464
+ * @returns {Object} Hessian matrix
465
+ */
466
+ export function hessian(func, variables) {
467
+ const hess = {};
468
+
469
+ for (const var1 of variables) {
470
+ hess[var1] = {};
471
+ for (const var2 of variables) {
472
+ let firstDeriv;
473
+
474
+ if (func.type === 'function') {
475
+ firstDeriv = differentiateFunction(func, var1);
476
+ } else {
477
+ firstDeriv = differentiatePolynomial(func, var1);
478
+ }
479
+
480
+ // Now differentiate the first derivative with respect to var2
481
+ if (firstDeriv.type === 'function') {
482
+ hess[var1][var2] = differentiateFunction(firstDeriv, var2);
483
+ } else {
484
+ hess[var1][var2] = differentiatePolynomial(firstDeriv, var2);
485
+ }
486
+ }
487
+ }
488
+
489
+ return hess;
490
+ }
491
+
492
+ /**
493
+ * Find tangent plane to surface z = f(x,y) at point (x0, y0)
494
+ * @param {Object} func - Function object representing f(x,y)
495
+ * @param {number} x0 - x-coordinate of point
496
+ * @param {number} y0 - y-coordinate of point
497
+ * @returns {Object} Tangent plane equation
498
+ */
499
+ export function tangentPlane(func, x0, y0) {
500
+ // Calculate partial derivatives at (x0, y0)
501
+ let fx, fy;
502
+
503
+ if (func.type === 'function') {
504
+ fx = differentiateFunction(func, 'x');
505
+ fy = differentiateFunction(func, 'y');
506
+ } else {
507
+ // Handle polynomial differentiation
508
+ fx = differentiatePolynomial(func, 'x');
509
+ fy = differentiatePolynomial(func, 'y');
510
+ }
511
+
512
+ // Evaluate function and derivatives at point
513
+ const z0 = evaluateFunction(func, { x: x0, y: y0 });
514
+ const fx0 = evaluateFunction(fx, { x: x0, y: y0 });
515
+ const fy0 = evaluateFunction(fy, { x: x0, y: y0 });
516
+
517
+ // Tangent plane: z = z0 + fx0*(x - x0) + fy0*(y - y0)
518
+ const plane = {
519
+ type: 'plane',
520
+ point: { x: x0, y: y0, z: z0 },
521
+ normal: { x: -fx0, y: -fy0, z: 1 },
522
+ equation: {
523
+ z: createTerm(z0),
524
+ x: createTerm(fx0, { x: 1 }),
525
+ y: createTerm(fy0, { y: 1 }),
526
+ constant: createTerm(-fx0 * x0 - fy0 * y0 + z0)
527
+ }
528
+ };
529
+
530
+ return plane;
531
+ }
532
+
533
+ /**
534
+ * Find tangent line to curve y = f(x) at point x0
535
+ * @param {Object} func - Function object
536
+ * @param {number} x0 - x-coordinate of point
537
+ * @returns {Object} Tangent line equation
538
+ */
539
+ export function tangentLine(func, x0) {
540
+ let derivative;
541
+
542
+ if (func.type === 'function') {
543
+ derivative = differentiateFunction(func, 'x');
544
+ } else {
545
+ // Handle polynomial differentiation
546
+ derivative = differentiatePolynomial(func, 'x');
547
+ }
548
+
549
+ const y0 = evaluateFunction(func, { x: x0 });
550
+ const slope = evaluateFunction(derivative, { x: x0 });
551
+
552
+ // Tangent line: y = y0 + slope*(x - x0)
553
+ const line = {
554
+ type: 'line',
555
+ point: { x: x0, y: y0 },
556
+ slope: slope,
557
+ equation: {
558
+ y: createTerm(slope, { x: 1 }),
559
+ constant: createTerm(y0 - slope * x0)
560
+ }
561
+ };
562
+
563
+ return line;
564
+ }
565
+
566
+ /**
567
+ * Find normal vector to surface at point
568
+ * @param {Object} func - Function object F(x,y,z) = 0
569
+ * @param {Object} point - Point {x, y, z}
570
+ * @returns {Object} Normal vector
571
+ */
572
+ export function surfaceNormal(func, point) {
573
+ const grad = gradient(func, ['x', 'y', 'z']);
574
+ const normal = {
575
+ x: evaluateFunction(grad.x, point),
576
+ y: evaluateFunction(grad.y, point),
577
+ z: evaluateFunction(grad.z, point)
578
+ };
579
+
580
+ // Normalize vector
581
+ const magnitude = Math.sqrt(normal.x ** 2 + normal.y ** 2 + normal.z ** 2);
582
+ return {
583
+ x: normal.x / magnitude,
584
+ y: normal.y / magnitude,
585
+ z: normal.z / magnitude
586
+ };
587
+ }
588
+
589
+ /**
590
+ * Find critical points of a multivariable function
591
+ * @param {Object} func - Function object
592
+ * @param {Array} variables - Array of variable names
593
+ * @returns {Array} Array of critical points
594
+ */
595
+ export function findCriticalPoints(func, variables) {
596
+ const grad = gradient(func, variables);
597
+ const criticalPoints = [];
598
+
599
+ // For simplicity, this is a basic implementation
600
+ // In practice, you'd solve the system of equations grad = 0
601
+ // This would require a numerical solver or symbolic equation solver
602
+
603
+ if (variables.length === 1) {
604
+ // Single variable case: solve f'(x) = 0
605
+ const derivative = grad[variables[0]];
606
+ // This is a placeholder - actual implementation would need root finding
607
+ criticalPoints.push({
608
+ type: 'critical_point',
609
+ variables: { [variables[0]]: 0 }, // Placeholder
610
+ value: evaluateFunction(func, { [variables[0]]: 0 })
611
+ });
612
+ } else if (variables.length === 2) {
613
+ // Two variable case: solve fx = 0, fy = 0
614
+ // Placeholder implementation
615
+ criticalPoints.push({
616
+ type: 'critical_point',
617
+ variables: { x: 0, y: 0 }, // Placeholder
618
+ value: evaluateFunction(func, { x: 0, y: 0 })
619
+ });
620
+ }
621
+
622
+ return criticalPoints;
623
+ }
624
+
625
+ /**
626
+ * Classify critical points using Hessian matrix
627
+ * @param {Object} func - Function object
628
+ * @param {Object} point - Critical point
629
+ * @returns {string} Classification: 'minimum', 'maximum', 'saddle', 'degenerate'
630
+ */
631
+ export function classifyCriticalPoint(func, point) {
632
+ const hess = hessian(func, Object.keys(point));
633
+ const variables = Object.keys(point);
634
+
635
+ if (variables.length === 1) {
636
+ // Single variable: use second derivative test
637
+ let secondDeriv;
638
+
639
+ if (func.type === 'function') {
640
+ const firstDeriv = differentiateFunction(func, variables[0]);
641
+ secondDeriv = differentiateFunction(firstDeriv, variables[0]);
642
+ } else {
643
+ const firstDeriv = differentiatePolynomial(func, variables[0]);
644
+ secondDeriv = differentiatePolynomial(firstDeriv, variables[0]);
645
+ }
646
+
647
+ const d2 = evaluateFunction(secondDeriv, point);
648
+
649
+ if (d2 > 0) return 'minimum';
650
+ if (d2 < 0) return 'maximum';
651
+ return 'degenerate';
652
+ } else if (variables.length === 2) {
653
+ // Two variables: use Hessian determinant test
654
+ const fxx = evaluateFunction(hess[variables[0]][variables[0]], point);
655
+ const fyy = evaluateFunction(hess[variables[1]][variables[1]], point);
656
+ const fxy = evaluateFunction(hess[variables[0]][variables[1]], point);
657
+
658
+ const D = fxx * fyy - fxy * fxy;
659
+
660
+ if (D > 0 && fxx > 0) return 'minimum';
661
+ if (D > 0 && fxx < 0) return 'maximum';
662
+ if (D < 0) return 'saddle';
663
+ return 'degenerate';
664
+ }
665
+
666
+ return 'unknown';
667
+ }
668
+
669
+ /**
670
+ * Find local maxima and minima of a function
671
+ * @param {Object} func - Function object
672
+ * @param {Array} variables - Array of variable names
673
+ * @param {Object} bounds - Optional bounds for search {x: [min, max], y: [min, max]}
674
+ * @returns {Object} Object with maxima and minima arrays
675
+ */
676
+ export function findExtrema(func, variables, bounds = {}) {
677
+ const criticalPoints = findCriticalPoints(func, variables);
678
+ const maxima = [];
679
+ const minima = [];
680
+
681
+ for (const point of criticalPoints) {
682
+ const classification = classifyCriticalPoint(func, point.variables);
683
+
684
+ if (classification === 'maximum') {
685
+ maxima.push(point);
686
+ } else if (classification === 'minimum') {
687
+ minima.push(point);
688
+ }
689
+ }
690
+
691
+ // Check boundaries if provided
692
+ if (Object.keys(bounds).length > 0) {
693
+ // This would require evaluating the function on boundary
694
+ // Placeholder implementation
695
+ }
696
+
697
+ return {
698
+ maxima,
699
+ minima,
700
+ critical_points: criticalPoints
701
+ };
702
+ }
703
+
704
+ /**
705
+ * Find global maximum and minimum in a region
706
+ * @param {Object} func - Function object
707
+ * @param {Array} variables - Array of variable names
708
+ * @param {Object} bounds - Bounds for each variable
709
+ * @returns {Object} Global extrema
710
+ */
711
+ export function findGlobalExtrema(func, variables, bounds) {
712
+ const localExtrema = findExtrema(func, variables, bounds);
713
+
714
+ let globalMax = null;
715
+ let globalMin = null;
716
+
717
+ // Check local extrema
718
+ for (const max of localExtrema.maxima) {
719
+ if (!globalMax || max.value > globalMax.value) {
720
+ globalMax = max;
721
+ }
722
+ }
723
+
724
+ for (const min of localExtrema.minima) {
725
+ if (!globalMin || min.value < globalMin.value) {
726
+ globalMin = min;
727
+ }
728
+ }
729
+
730
+ // Check boundaries (simplified)
731
+ // In practice, you'd need to evaluate on all boundary combinations
732
+
733
+ return {
734
+ global_maximum: globalMax,
735
+ global_minimum: globalMin,
736
+ local_extrema: localExtrema
737
+ };
738
+ }
739
+
740
+ /**
741
+ * Find extrema subject to constraints using Lagrange multipliers
742
+ * @param {Object} func - Objective function
743
+ * @param {Array} constraints - Array of constraint functions
744
+ * @param {Array} variables - Array of variable names
745
+ * @returns {Array} Array of constrained extrema
746
+ */
747
+ export function lagrangeMultipliers(func, constraints, variables) {
748
+ const constrainedExtrema = [];
749
+
750
+ // For each constraint, create Lagrangian: L = f - λ*g
751
+ // Then solve ∇L = 0
752
+ // This is a placeholder - actual implementation would require solving systems
753
+
754
+ for (let i = 0; i < constraints.length; i++) {
755
+ const constraint = constraints[i];
756
+
757
+ // Placeholder solution
758
+ constrainedExtrema.push({
759
+ type: 'constrained_extremum',
760
+ constraint_index: i,
761
+ variables: variables.reduce((obj, v) => ({ ...obj, [v]: 0 }), {}),
762
+ value: evaluateFunction(func, variables.reduce((obj, v) => ({ ...obj, [v]: 0 }), {})),
763
+ multiplier: 0 // λ
764
+ });
765
+ }
766
+
767
+ return constrainedExtrema;
768
+ }
769
+
770
+ /**
771
+ * Calculate directional derivative
772
+ * @param {Object} func - Function object
773
+ * @param {Object} point - Point of evaluation
774
+ * @param {Object} direction - Direction vector (not necessarily unit)
775
+ * @returns {number} Directional derivative
776
+ */
777
+ export function directionalDerivative(func, point, direction) {
778
+ const variables = Object.keys(point);
779
+ const grad = gradient(func, variables);
780
+
781
+ // Calculate magnitude of direction vector
782
+ const dirMag = Math.sqrt(Object.values(direction).reduce((sum, val) => sum + val ** 2, 0));
783
+
784
+ // Normalize direction vector
785
+ const unitDir = {};
786
+ for (const [varName, val] of Object.entries(direction)) {
787
+ unitDir[varName] = val / dirMag;
788
+ }
789
+
790
+ // Calculate dot product of gradient and unit direction
791
+ let dotProduct = 0;
792
+ for (const varName of variables) {
793
+ const gradVal = evaluateFunction(grad[varName], point);
794
+ dotProduct += gradVal * unitDir[varName];
795
+ }
796
+
797
+ return dotProduct;
798
+ }
799
+
800
+ /**
801
+ * Find direction of steepest ascent/descent
802
+ * @param {Object} func - Function object
803
+ * @param {Object} point - Point of evaluation
804
+ * @returns {Object} Directions of steepest ascent and descent
805
+ */
806
+ export function steepestDirections(func, point) {
807
+ const variables = Object.keys(point);
808
+ const grad = gradient(func, variables);
809
+
810
+ // Gradient points in direction of steepest ascent
811
+ const ascent = {};
812
+ const descent = {};
813
+
814
+ for (const varName of variables) {
815
+ const gradVal = evaluateFunction(grad[varName], point);
816
+ ascent[varName] = gradVal;
817
+ descent[varName] = -gradVal;
818
+ }
819
+
820
+ return {
821
+ steepest_ascent: ascent,
822
+ steepest_descent: descent,
823
+ gradient_magnitude: Math.sqrt(Object.values(ascent).reduce((sum, val) => sum + val ** 2, 0))
824
+ };
825
+ }
826
+
827
+ /**
828
+ * Convert tangent plane/line to LaTeX
829
+ * @param {Object} tangent - Tangent object from tangentPlane or tangentLine
830
+ * @returns {string} LaTeX representation
831
+ */
832
+ export function tangentToLatex(tangent) {
833
+ if (tangent.type === 'plane') {
834
+ const { equation } = tangent;
835
+ const zTerm = slangToLatex(equation.z);
836
+ const xTerm = slangToLatex(equation.x);
837
+ const yTerm = slangToLatex(equation.y);
838
+ const constTerm = slangToLatex(equation.constant);
839
+
840
+ return `z = ${zTerm} + ${xTerm} + ${yTerm} + ${constTerm}`;
841
+ } else if (tangent.type === 'line') {
842
+ const { equation } = tangent;
843
+ const yTerm = slangToLatex(equation.y);
844
+ const constTerm = slangToLatex(equation.constant);
845
+
846
+ return `y = ${yTerm} + ${constTerm}`;
847
+ }
848
+
849
+ return '';
850
+ }
851
+
435
852
  // ============================================================================
436
853
  // EXAMPLES AND DEMO
437
854
  // ============================================================================
@@ -496,6 +913,21 @@ export default {
496
913
  getFunctionLatex,
497
914
  SUPPORTED_FUNCTIONS,
498
915
 
916
+ // Advanced Mathematical Functions
917
+ gradient,
918
+ hessian,
919
+ tangentPlane,
920
+ tangentLine,
921
+ surfaceNormal,
922
+ findCriticalPoints,
923
+ classifyCriticalPoint,
924
+ findExtrema,
925
+ findGlobalExtrema,
926
+ lagrangeMultipliers,
927
+ directionalDerivative,
928
+ steepestDirections,
929
+ tangentToLatex,
930
+
499
931
  // Demo
500
932
  demoExtendedFunctions
501
933
  };
package/slang-math.js CHANGED
@@ -1,3 +1,11 @@
1
- export * from "./slang-basic.js";
2
- export * from "./slang-advanced.js";
3
- export * from "./slang-helpers.js";
1
+ /**
2
+ * SLaNg Math Module
3
+ * Central export point for all SLaNg mathematical functions
4
+ */
5
+
6
+ // Export all functions from other modules
7
+ export * from "./slang-basic.js";
8
+ export * from "./slang-advanced.js";
9
+ export * from "./slang-helpers.js";
10
+ export * from "./slang-convertor.js";
11
+ export * from "./slang-extended.js";