or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-3/

Algebraic Expression Simplifier with Context Awareness

Build a JavaScript/TypeScript function that simplifies algebraic expressions with context-aware assumptions about variables.

Background

When simplifying algebraic expressions, different assumptions about variables lead to different results. For example, sqrt(x^2) simplifies to x if x is positive, but to abs(x) in general. Your task is to implement a function that simplifies expressions while respecting variable constraints.

Requirements

Implement a function simplifyWithContext(expression, variables) that:

  1. Takes a mathematical expression as a string
  2. Takes an object mapping variable names to their context properties:
    • real: boolean indicating if the variable is a real number
    • positive: boolean indicating if the variable is positive
    • complex: boolean indicating if the variable can be complex
  3. Returns the simplified expression as a string
  4. Applies appropriate simplification rules based on the context

The function should handle expressions involving:

  • Square roots and powers
  • Absolute values
  • Algebraic operations (addition, subtraction, multiplication, division)
  • Multiple variables with different contexts

Example Usage

// Example 1: Simplify sqrt(x^2) assuming x is positive
simplifyWithContext('sqrt(x^2)', { x: { positive: true } })
// Expected: 'x'

// Example 2: Simplify sqrt(x^2) assuming x is real (but not necessarily positive)
simplifyWithContext('sqrt(x^2)', { x: { real: true } })
// Expected: 'abs(x)'

// Example 3: Simplify sqrt(x^2) for complex x
simplifyWithContext('sqrt(x^2)', { x: { complex: true } })
// Expected: expression involving complex square root logic

// Example 4: Multiple variables with contexts
simplifyWithContext('sqrt(a^2) + sqrt(b^2)', {
  a: { positive: true },
  b: { real: true }
})
// Expected: 'a + abs(b)'

Implementation Details

  1. Create a file named simplifier.js (or simplifier.ts for TypeScript)
  2. Export the simplifyWithContext function
  3. Handle edge cases like invalid expressions or missing context information
  4. The function should return simplified expressions in a standard mathematical notation

Test Cases { .test-cases }

Test Case 1: Positive Variable { @test }

File: simplifier.test.js

import { simplifyWithContext } from './simplifier.js';

// Simplifying sqrt(x^2) with positive x should return x
const result = simplifyWithContext('sqrt(x^2)', { x: { positive: true } });
console.assert(result === 'x', `Expected 'x', got '${result}'`);

Test Case 2: Real Variable { @test }

File: simplifier.test.js

import { simplifyWithContext } from './simplifier.js';

// Simplifying sqrt(x^2) with real x should return abs(x)
const result = simplifyWithContext('sqrt(x^2)', { x: { real: true } });
console.assert(result === 'abs(x)', `Expected 'abs(x)', got '${result}'`);

Test Case 3: Multiple Variables { @test }

File: simplifier.test.js

import { simplifyWithContext } from './simplifier.js';

// Simplifying expression with multiple variables
const result = simplifyWithContext('sqrt(a^2 + 2*a*b + b^2)', {
  a: { positive: true },
  b: { positive: true }
});
// Should simplify (a+b)^2 under square root to a+b
console.assert(result === 'a + b', `Expected 'a + b', got '${result}'`);

Dependencies { .dependencies }

mathjs { .dependency }

Provides algebraic simplification and expression parsing capabilities.

Constraints

  • Use only standard JavaScript/TypeScript features and the allowed dependency
  • Handle malformed expressions gracefully
  • Ensure the simplified expression is mathematically equivalent under the given context
  • Support at least the three contexts: real, positive, and complex