Linear combination of two vectors

Executive summary

Given input representing two vectors and their respective "weights", produce output that also represents the weighted sum of those vectors.

Challenge

The input will consist of one or more lines of the following characters:

  • exactly one occurrence of the digit 0, which represents the origin in a two-dimensional plane;
  • exactly two other digits (1-9; may or may not be the same digit), whose positions relative to the origin represent vectors, and whose values represent the weights attached to thse vectors;
  • some number of "background characters". The solver may choose a specific background character; for example, I will choose "." (mostly for human readability). Alternately, the background characters can be anything that look like blank space.

(The solver can choose whether the input is a single multi-line string or an array of one-line strings.)

For example, the input

....2 .0... ...3. 

represents a vector at coordinates (3,1) with weight 2, and a vector at coordinates (2,-1) with weight 3.

The output should be almost the same as the input, with the following changes:

  • a "result character", chosen by the solver, to be added at the position specified by weighted sum of the input vectors (equivalently, at the position that is the appropriate linear combination of the input vectors);
  • as many background characters as are needed to fit the origin, the two input vectors, and the output vector in the same picture. Extra background characters can be included if desired; the only constraint is that, if the background character is a visible character, then the entire output must be rectangular in shape and every character not representing a vector must be the background character. (If blank space is used as background characters, then these constraints don't need to be enforced.)

(In general, if we have one vector (v,w) with weight a and second vector (x,y) with weight b, their weighted sum is a(v,w)+b(x,y) = (av+bx,aw+by).)

In the previous example, the appropriate linear combination is 2*(3,1) + 3*(2,-1) = (12,-1). If we use "X" as the result character, then the output could look like

....2......... .0............ ...3.........X 

or

................ ...2............ 0............... ..3.........X... ................ ................ 

Usual code-golf scoring: the shortest answer, in bytes, wins.

Example input and output

If blank space is used, the above input would look like

    2  0    3 

and the output would look like

    2  0    3         X 

Leading/trailing whitespace characters/lines are irrelevant; if they're invisible to the reader, it's fine. (That being said, for the rest of the examples I'll go back to using "." for the background character, to make it easier to read.)

If both vectors have weight 1, then the result will look like a parallelogram: the input

.1. ... 1.0 

leads to the output

X.1. .... .1.0 

Note this parallelogram can be degenerate if the input vectors are collinear: the input

0.1..1 

leads to the output

0.1..1.1 

It is possible for the result vector to equal one of the input vectors or the origin; in this case, it simply overwrites the input character. For example, the input

..2.0.1... 

yields the output

..X.0.1... 

(where in input and/or output, the leading and trailing periods could be deleted). The input

.....3 ...... ...0.. ...... ...... 2..... 

yields the output

.....3 ...... ...X.. ...... ...... 2..... 

Finally, the input

90 .8 

yields the output

........90 .........8 .......... .......... .......... .......... .......... .......... X......... 

Replay

MATL, 48 bytes

tZyyX:UX>*Yat48-tt0>*3#fbbhb~2#fh-*s7M+'X'wZ}4$(

The background character is space. Input is a 2D char array with rows separated by semicolons. So the tests cases have respective inputs:

['    2'; ' 0   '; '   3 ']
[' 1 '; '   '; '1 0']
['0 1  1']
['  2 0 1   ']
['     3'; '      '; '   0  '; '      '; '      '; '2     ']
['90'; ' 8']

The output includes a significant amount of padding whitespace.

Try it online!

Category: code golf Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.132 (s). 12 q(s)