How to resolve the algorithm Display a linear combination step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Display a linear combination step by step in the EchoLisp programming language

Table of Contents

Problem Statement

Display a finite linear combination in an infinite vector basis

(

e

1

,

e

2

, … )

{\displaystyle (e_{1},e_{2},\ldots )}

. Write a function that, when given a finite list of scalars

(

α

1

,

α

2

, … )

{\displaystyle (\alpha ^{1},\alpha ^{2},\ldots )}

, creates a string representing the linear combination

i

α

i

e

i

{\displaystyle \sum {i}\alpha ^{i}e{i}}

in an explicit format often used in mathematics, that is: where

α

i

k

≠ 0

{\displaystyle \alpha ^{i_{k}}\neq 0}

The output must comply to the following rules:

Show here output for the following lists of scalars:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Display a linear combination step by step in the EchoLisp programming language

Source code in the echolisp programming language

;; build an html string from list of coeffs

(define (linear->html coeffs)
    (define plus #f) 
    (or* 
    (for/fold (html "") ((a coeffs) (i (in-naturals 1)))
      (unless (zero? a)
 		(set! plus (if plus "+" "")))
      (string-append html
	 (cond 
	  ((= a 1)  (format "%a e%d " plus i))
	  ((= a -1) (format "- e%d " i))
	  ((> a 0)  (format "%a %d*e%d " plus a i))
	  ((< a 0)  (format "- %d*e%d " (abs a) i))
	  (else ""))))
     "0"))
	
(define linears '((1 2 3)
   (0 1 2 3)
   (1 0 3 4)
   (1 2 0)
   (0 0 0)
   (0)
   (1 1 1)
   (-1 -1 -1)
   (-1 -2 0 -3)
   (-1)))
   
(define (task linears)
    (html-print ;; send string to stdout
    (for/string ((linear linears))
      (format "%a -> %a 
" linear (linear->html linear)))))

You may also check:How to resolve the algorithm Date format step by step in the D programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the VBA programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Racket programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the RPL programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Delphi programming language