Display a linear combination: Difference between revisions
(change task description for better consistency between scalar indices and vector indices : indices begin at one, not zero) |
(J draft) |
||
Line 22: | Line 22: | ||
9) -1, -2, 0, -3 |
9) -1, -2, 0, -3 |
||
10) -1</pre> |
10) -1</pre> |
||
=={{header|J}}== |
|||
Implementation: |
|||
<lang J>fourbanger=:3 :0 |
|||
e=. ('e(',')',~])@":&.> 1+i.#y |
|||
firstpos=. 0< {.y-.0 |
|||
if. */0=y do. '0' else. firstpos}.;y gluedto e end. |
|||
) |
|||
gluedto=:4 :0 each |
|||
pfx=. '+-' {~ x<0 |
|||
select. |x |
|||
case. 0 do. '' |
|||
case. 1 do. pfx,y |
|||
case. do. pfx,(":|x),'*',y |
|||
end. |
|||
)</lang> |
|||
Example use: |
|||
<lang J> fourbanger 1 2 3 |
|||
e(1)+2*e(2)+3*e(3) |
|||
fourbanger 0 1 2 3 |
|||
e(2)+2*e(3)+3*e(4) |
|||
fourbanger 1 0 3 4 |
|||
e(1)+3*e(3)+4*e(4) |
|||
fourbanger 0 0 0 |
|||
0 |
|||
fourbanger 0 |
|||
0 |
|||
fourbanger 1 1 1 |
|||
e(1)+e(2)+e(3) |
|||
fourbanger _1 _1 _1 |
|||
-e(1)-e(2)-e(3) |
|||
fourbanger _1 _2 0 _3 |
|||
-e(1)-2*e(2)-3*e(4) |
|||
fourbanger _1 |
|||
-e(1)</lang> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
Revision as of 16:20, 13 October 2015
The purpose of this task is to display a finite linear combination in an infinite vector basis .
Write a function that, when given a finite list of scalars , creates a string representing the linear combination
Your output must comply to the following rules:
- you don't show null terms, unless the whole combination is null. For instance while the output "0" is fine, "e(1) + 0*e(3)" or "0 + e(1)" are wrong.
- you don't show scalars when they are equal to one or minus one. For instance the string "1*e(3)" is wrong.
- you don't prefix by '-' if it follows a preceding term. Instead you use subtraction. Thus "e(4) - e(5)" is correct while "e(4) + -e(5)" is wrong.
Show here output for the following lists of scalars:
1) 1, 2, 3 2) 0, 1, 2, 3 3) 1, 0, 3, 4 4) 1, 2, 0 5) 0, 0, 0 6) 0 7) 1, 1, 1 8) -1, -1, -1 9) -1, -2, 0, -3 10) -1
J
Implementation:
<lang J>fourbanger=:3 :0
e=. ('e(',')',~])@":&.> 1+i.#y firstpos=. 0< {.y-.0 if. */0=y do. '0' else. firstpos}.;y gluedto e end.
)
gluedto=:4 :0 each
pfx=. '+-' {~ x<0 select. |x case. 0 do. case. 1 do. pfx,y case. do. pfx,(":|x),'*',y end.
)</lang>
Example use:
<lang J> fourbanger 1 2 3 e(1)+2*e(2)+3*e(3)
fourbanger 0 1 2 3
e(2)+2*e(3)+3*e(4)
fourbanger 1 0 3 4
e(1)+3*e(3)+4*e(4)
fourbanger 0 0 0
0
fourbanger 0
0
fourbanger 1 1 1
e(1)+e(2)+e(3)
fourbanger _1 _1 _1
-e(1)-e(2)-e(3)
fourbanger _1 _2 0 _3
-e(1)-2*e(2)-3*e(4)
fourbanger _1
-e(1)</lang>
Perl 6
<lang perl6>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)\ .grep(+*.key)\ .map({ .key ~ '*' ~ .value })\ .join(' + ')\ .subst('+ -', '- ', :g)\ .subst(/<|w>1\*/, , :g) || '0'
}
say linear-combination($_) for [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 ]
- </lang>
- Output:
e(1) + 2*e(2) + 3*e(3) e(2) + 2*e(3) + 3*e(4) e(1) + 3*e(3) + 4*e(4) e(1) + 2*e(2) 0 0 e(1) + e(2) + e(3) -e(1) - e(2) - e(3) -e(1) - 2*e(2) - 3*e(4) -e(1)
Tcl
This solution strives for legibility rather than golf.
<lang Tcl>proc lincom {factors} {
set exp 0 set res "" foreach f $factors { incr exp if {$f == 0} { continue } elseif {$f == 1} { append res "+e($exp)" } elseif {$f == -1} { append res "-e($exp)" } elseif {$f > 0} { append res "+$f*e($exp)" } else { append res "$f*e($exp)" } } if {$res eq ""} {set res 0} regsub {^\+} $res {} res return $res
}
foreach test {
{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}
} {
puts [format "%10s -> %-10s" $test [lincom $test]]
}</lang>
- Output:
1 2 3 -> e(1)+2*e(2)+3*e(3) 0 1 2 3 -> e(2)+2*e(3)+3*e(4) 1 0 3 4 -> e(1)+3*e(3)+4*e(4) 1 2 0 -> e(1)+2*e(2) 0 0 0 -> 0 0 -> 0 1 1 1 -> e(1)+e(2)+e(3) -1 -1 -1 -> -e(1)-e(2)-e(3) -1 -2 0 -3 -> -e(1)-2*e(2)-3*e(4) -1 -> -e(1)
zkl
<lang zkl>fcn linearCombination(coeffs){
[1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs) .filter().concat("+").replace("+-","-").replace("1*","") or 0
}</lang> <lang zkl>T(T(1,2,3),T(0,1,2,3),T(1,0,3,4),T(1,2,0),T(0,0,0),T(0),T(1,1,1),T(-1,-1,-1),
T(-1,-2,0,-3),T(-1),T)
.pump(Console.println,linearCombination);</lang>
- Output:
e(1)+2*e(2)+3*e(3) e(2)+2*e(3)+3*e(4) e(1)+3*e(3)+4*e(4) e(1)+2*e(2) 0 0 e(1)+e(2)+e(3) -e(1)-e(2)-e(3) -e(1)-2*e(2)-3*e(4) -e(1) 0