Skip to main content
Glama
nbiish
by nbiish

riemann_sum

Calculate the Riemann sum of a function to approximate definite integrals using left, right, midpoint, or trapezoid methods.

Instructions

Calculate the Riemann sum of a function using different methods

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expressionYesFunction to integrate
variableYesVariable of integration
aYesLower limit of integration
bYesUpper limit of integration
nYesNumber of subintervals
methodNoMethod: left, right, midpoint, or trapezoidmidpoint

Implementation Reference

  • Handler function that receives tool inputs and delegates to the riemannSum helper function to compute the Riemann sum.
    async ({ expression, variable, a, b, n, method }) => {
      return riemannSum(expression, variable, a, b, n, method);
    }
  • Zod schemas defining the input parameters and numeric output for the riemann_sum tool.
    inputSchema: z.object({
      expression: z.string().describe('Function to integrate'),
      variable: z.string().describe('Variable of integration'),
      a: z.number().describe('Lower limit of integration'),
      b: z.number().describe('Upper limit of integration'),
      n: z.number().describe('Number of subintervals'),
      method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint')
        .describe('Method: left, right, midpoint, or trapezoid')
    }),
    outputSchema: z.number(),
  • index.js:110-128 (registration)
    Complete registration of the 'riemann_sum' tool using Genkit's ai.defineTool, including name, description, schema, and handler.
    ai.defineTool(
      {
        name: 'riemann_sum',
        description: 'Calculate the Riemann sum of a function using different methods',
        inputSchema: z.object({
          expression: z.string().describe('Function to integrate'),
          variable: z.string().describe('Variable of integration'),
          a: z.number().describe('Lower limit of integration'),
          b: z.number().describe('Upper limit of integration'),
          n: z.number().describe('Number of subintervals'),
          method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint')
            .describe('Method: left, right, midpoint, or trapezoid')
        }),
        outputSchema: z.number(),
      },
      async ({ expression, variable, a, b, n, method }) => {
        return riemannSum(expression, variable, a, b, n, method);
      }
    );
  • Core helper function that performs the actual Riemann sum calculation using mathjs for expression parsing and evaluation, supporting left, right, midpoint, and trapezoid methods.
    const riemannSum = (expr, variable, a, b, n, method = 'midpoint') => {
      try {
        const deltaX = (b - a) / n;
        let sum = 0;
        const node = math.parse(expr);
        const scope = {};
        
        if (method === 'left' || method === 'right') {
          const offset = method === 'right' ? 1 : 0;
          for (let i = 0; i < n; i++) {
            const x = a + (i + offset) * deltaX;
            scope[variable] = x;
            sum += math.evaluate(node, scope) * deltaX;
          }
        } else if (method === 'midpoint') {
          for (let i = 0; i < n; i++) {
            const x = a + (i + 0.5) * deltaX;
            scope[variable] = x;
            sum += math.evaluate(node, scope) * deltaX;
          }
        } else if (method === 'trapezoid') {
          for (let i = 0; i <= n; i++) {
            const x = a + i * deltaX;
            scope[variable] = x;
            const coef = (i === 0 || i === n) ? 0.5 : 1;
            sum += coef * math.evaluate(node, scope) * deltaX;
          }
        }
        
        return sum;
      } catch (e) {
        return `Error: ${e.message}`;
      }
    };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nbiish/mcp-calc-tools'

If you have feedback or need assistance with the MCP directory API, please join our Discord server