6
\$\begingroup\$

There has not been a challenge regarding slope fields, as a far as I can tell. So, I might as well make one.

The challenge

Given:

  • A black box function f which takes two arguments, x and y (both real numbers) , and returns the value of the slope at point (x,y) (also a real number)
  • A list of real numbers, X, representing the values along the x axis,
  • Another list of real numbers, Y, representing the values along the y axis.
  • A list of (x,y) pairs, U, which represents the inputs that f is undefined on. This argument can be removed if your language can detect errors.

Output:

  • A rectangle with a length of the length X, and a height of the length of Y. At each combination of row r and column c in the rectangle, (starting from the bottom left corner), the character that is placed at that position will depend on the sign value of f evaluated at x=c and y=r:
    • If the value is positive, the character is /.
    • If the value is zero, the character is -.
    • If the value is negative, the character is \.
    • If (x,y) is not in the domain of f (a.k.a. a member of U), then the character is . f is guaranteed to not error on every combination of x within X and y within Y if U is utilized.

Since I am horrible at explaining things, here's an example:

Input:

f(x,y) = x / y
X=[-1,0,1]
Y=[-1,0,1] 
U=[[-1,0],[0,0],[1,0]]

Output:

\-/

/-\
  • At (0,0) (the bottom left corner), the corresponding X and Y values are -1 and -1 (since X[0] = -1 and Y[0] = -1). f(-1,-1)=(-1)/(-1)=1, thus / is used.
  • At (1,0) (the bottom row, middle column): X[1] = 0, Y[0] = -1, f(0,-1)=(0)/(-1)=0, and 0 is zero (duh), so the character is -.
  • At (1,1) (the center): X[1] = 0, Y[1] = 0. [0,0] is a member of U. Therefore, it corresponds to the character. Or, if done without U: 0/0 is undefined, thus the character is .
  • At (2,2) (the top left): X[2] = 1, Y[2] = 1, f(1,1)=(1)/(1)=1, which is positive. Therefore, it corresponds to the / character.
  • Etc...

Floating Point

Any value of f within the range [-1e-9,1e-9] should be considered to have a sign of 0.

Winning condition

This is a , so lowest byte count wins!

\$\endgroup\$
  • \$\begingroup\$ Are the inputs sorted? \$\endgroup\$ – Giuseppe Dec 12 '17 at 15:57
  • \$\begingroup\$ @Giuseppe No. And unsorted X and Y will actually change the answer. \$\endgroup\$ – Zacharý Dec 12 '17 at 21:00
6
\$\begingroup\$

Jelly, 23 bytes

Ṛ⁹,2⁶v$Ṡ$e⁵$?¥þị“/ \-”Y

Try it online!

Arguments: Y, X, U, f

f is a Python string containing Jelly code. Be sure to quote it appropriately, otherwise you may encounter errors.

Also, f takes a pair [x, y] as its argument, not two arguments x and y.

\$\endgroup\$
  • \$\begingroup\$ Can you please provide an explanation for those of us who have to constantly refer to the wiki? \$\endgroup\$ – Zacharý Dec 12 '17 at 15:05
  • \$\begingroup\$ @Zacharý sorry, no time to \$\endgroup\$ – Erik the Outgolfer Dec 12 '17 at 15:09
  • \$\begingroup\$ Whenever you get the chance \$\endgroup\$ – Zacharý Dec 12 '17 at 17:17
  • \$\begingroup\$ Explanation (May not be 100% correct) \$\endgroup\$ – caird coinheringaahing Dec 12 '17 at 17:51
3
\$\begingroup\$

APL (Dyalog), 36 34 bytes

2 bytes saved thanks to @ngn

{o←⍺⍺⋄'\-/ '[⊖⍉⍺∘.{0::4⋄2+×⍺o⍵}⍵]}

Try it online! (with modified division, since basic APL 0÷0 is 1)

The black box function comes as left operand, X as left argument, and Y as right argument.

\$\endgroup\$
  • \$\begingroup\$ ideas: take x and y as ⍺ and ⍵, not just ⍵ (you can still have the ⍺⍺); in the inner dfn return 2+×⍺o⍵ on success and 4 on error and then do square bracket indexing on the whole matrix in order to save some quotes \$\endgroup\$ – ngn Dec 10 '17 at 15:04
  • \$\begingroup\$ @ngn Looks like it already does most of what you've mentioned? \$\endgroup\$ – Erik the Outgolfer Dec 10 '17 at 15:05
  • \$\begingroup\$ @EriktheOutgolfer did you notice there's an inner { }? \$\endgroup\$ – ngn Dec 10 '17 at 15:19
  • \$\begingroup\$ @ngn problematic with complex results (2J1s) \$\endgroup\$ – Uriel Dec 10 '17 at 15:59
  • \$\begingroup\$ @Uriel the problem says f "returns the value of the slope at point (x,y) (also a real number)" \$\endgroup\$ – ngn Dec 10 '17 at 16:03
2
\$\begingroup\$

Haskell, 114 113 109 bytes

z x=last$0:[x|abs x>1e-9]
(x#y)(%)u=reverse[[last$"\\-/"!!floor(1+signum(z$c%r)):[' '|elem(c,r)u]|c<-x]|r<-y]

Try it online!

Fairly straightforward solution. Uses signum to index into a string for the right char if the point is defined.

EDIT: Thought of a way to shave off a byte

EDIT 2: Thanks @Laikoni for taking off another 4 bytes!

\$\endgroup\$
  • 1
    \$\begingroup\$ Taking f as third argument and as infix function saves another byte: Try it online! \$\endgroup\$ – Laikoni Dec 12 '17 at 21:59
  • 1
    \$\begingroup\$ Save some more bytes by using elem instead of notElem: Try it online! \$\endgroup\$ – Laikoni Dec 12 '17 at 22:02
1
\$\begingroup\$

Swift, 142 bytes

func f(X:[Float],Y:[Float]){print(Y.reversed().flatMap{y in X.flatMap{let s=try?b($0,y);return s==nil ?" ":s!>0 ?"/":s!<0 ?"\\":"-"}+["\n"]})}

Prints as an array of characters

\$\endgroup\$
1
\$\begingroup\$

Python 2, 91 bytes

f,X,Y,U=input()
for y in Y[::-1]:print''.join('/ \-'[[x,y]in U or~cmp(0,f(x,y))]for x in X)

Try it online!

-3 thanks to Jonathan Allan.

Looks like I can't just assume f to be already assigned to a function.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 105 103 95 92 bytes

def f(F,X,Y,U):
 for y in Y[::-1]:print''.join('/ \-'[[x,y]in U or~cmp(0,F(x,y))]for x in X)

Try it online!


Error handling version:

Python 2, 135 132 125 bytes

def f(F,X,Y):
 def g(x,y):
	try:return'-\/'[cmp(0,F(x,y))]
	except:return' '
 for y in Y[::-1]:print''.join(g(x,y)for x in X)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ You can save the same three bytes I saved for Erik by replacing ([x,y]in U)*' 'or'-\/'[cmp(0,F(x,y)) with '/ \-'[[x,y]in U or~cmp(0,f(x,y)) :) \$\endgroup\$ – Jonathan Allan Dec 10 '17 at 19:11
  • \$\begingroup\$ @JonathanAllan Thanks :) \$\endgroup\$ – TFeld Dec 11 '17 at 7:54
1
\$\begingroup\$

Wolfram Language (Mathematica), 70 68 bytes

Table["-"["/","\\"][[Sign[x~#~y]]]~Check~" ",{y,Reverse@#3},{x,#2}]&

Try it online!

In the expression "-"["/","\\"], part 0 is the head ("-"), part 1 is "/", and part -1 (the last part) is "\\", so if we try to take the Sign[f]-th part of it, we get the appropriate character depending on if f is positive, negative, or zero. If none of the above apply, or if evaluating the function causes an error, the ~Check~ will catch the error and return the " " character instead.

(It still prints out a bunch of error messages, which should be ignored since we get the right answer at the end.)

We do this for all x and y values.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.