Write a Sine-Deriving Machine

6

1

The Challenge

Your challenge is to write a program that evaluates the following function:

f(x, t) = d^x / dt^x (sin t)

That is, the x-th derivative of sin t. In case you aren't familiar with calculus, here is a table of derivatives for the sine function:

...

f(-1, t) = -cos t

f(0, t) = sin t

f(1, t) = cos t

f(2, t)= -sin t

...

The sequence repeats these 4 functions.

The Inputs

Your program should take in an integer to be used as the x value in the function, and a float to be used at the t value in the function.

Your program may use radians or degrees for the sine function.

The Output

Your program should output the value of f(x, t) given the integer x and float t.

Scoring

This is , so the shortest answer (in bytes) wins.

Meow Mix

Posted 2016-08-13T18:44:12.947

Reputation: 823

6Are you sure d^-1 / dt^-1 (sin t) = -cos t ? – Leaky Nun – 2016-08-13T18:52:30.017

1What precision should this be to? – HyperNeutrino – 2016-08-13T18:58:37.137

1Test cases would be great. – Lynn – 2016-08-13T22:26:07.110

Okay so trigonometry builtins seem to be allowed. Just a comment, who is not familiar with calculus does not understand the table either :P – Leif Willerts – 2016-08-14T00:46:27.667

Today, I am more in the mood of writing an exponential function e^x deriving machine. – sergiol – 2017-05-26T21:37:30.030

I think you should repeat the challenge forbidding the π/2 left shift technique! – sergiol – 2017-05-27T20:40:09.990

Answers

6

Actually, 5 bytes

╦*½+S

Try it online

Takes input as t\nx.

Explanation:

╦*½+S
╦*     pi*n
  ½    half that
   +   add t
    S  sine

Mego

Posted 2016-08-13T18:44:12.947

Reputation: 32 998

11

Python, 42 bytes

from math import*
lambda x,n:sin(x+pi/2*n)

Uses the fact that differentiating shifts the function by pi/2 left.

xnor

Posted 2016-08-13T18:44:12.947

Reputation: 115 687

2I'm such an idiot – Leaky Nun – 2016-08-13T19:12:47.903

6

C (gcc), 30 bytes

#define f(x,n)sin(x+n*acos(0))

orlp

Posted 2016-08-13T18:44:12.947

Reputation: 37 067

4

Haskell, 16 bytes

x%n=sin$x+pi/2*n

Defines a binary operator %.

λ 2.0 % (-3)
-0.4161468365471426

Uses the fact that differentiating shifts the function by pi/2 left.

Making it point-free was longer (takes n x):

(sin.).(+).(pi/2*)

xnor

Posted 2016-08-13T18:44:12.947

Reputation: 115 687

4

Convex (IDE version only), 6 bytes

P½*+ms

Try it online!

After futher investigation, with the help of Dennis in chat, it was found that the command-line version of Convex (what TIO uses) has a bug that breaks the code (the link above contains a workaround). However, the IDE version of Convex, avaliable on GitHub before the challenge was posted, does not have this bug. Here is a link to the IDE version that was avaliable before the challenge was posted: Convex IDE.

GamrCorps

Posted 2016-08-13T18:44:12.947

Reputation: 7 058

3

Mathematica 14 bytes

With help from Martin Ender,

Sin[#2+Pi/2#]&

DavidC

Posted 2016-08-13T18:44:12.947

Reputation: 24 524

3Shouldn't x be #2? – Martin Ender – 2016-08-13T19:54:02.653

3At the same time, you don't need the *, so you'll end up with the same byte count. – Martin Ender – 2016-08-13T21:14:58.553

2

J, 17 bytes

4 :'1&o.d.(4|x)y'

Computes the (x%4)th derivative of sine, and then apply to right argument.

Usage:

>> f =: 4 :'1&o.d.(4|x)y'
>> _1 0 1 2 3 (f"0 0) 0
<< _1 0 1 0 _1

where >> is STDIN and << is STDOUT.

Leaky Nun

Posted 2016-08-13T18:44:12.947

Reputation: 45 011

Why do you need to compute the (x%4)? Wouldn't it be simpler to just use the xth derivative? (Not a J user so this might be a stupid question) – Steven H. – 2016-08-13T22:45:49.710

@StevenH. Because stupid question requires negative numbers. – Leaky Nun – 2016-08-14T02:58:50.597

2

Julia, 17 bytes

x\t=x*π/2+t|>sin

Try it online!

Dennis

Posted 2016-08-13T18:44:12.947

Reputation: 196 637

1

C (gcc), 53 46 45 bytes

#import<math.h>
#define f(x,n)sin(x+M_PI/2*n)

Probably Ideone doesn't have glibc, says M_PI is undeclared.

betseg

Posted 2016-08-13T18:44:12.947

Reputation: 8 493

M_PI is not in math.h: http://stackoverflow.com/questions/5007925/using-m-pi-with-c89-standard. – orlp – 2016-08-13T23:00:06.627

1Not in "C", the language you specified in the top of your answer. C as implemented by gcc has it, so you can write "C (gcc)", but C is a language defined by an abstract standard, and makes no mention of M_PI. – orlp – 2016-08-13T23:24:29.207

1

R, 26 bytes

function(t,x)sin(t+pi/2*x)

Exactly what it says on the tin...

Andreï Kostyrka

Posted 2016-08-13T18:44:12.947

Reputation: 1 389

1

JavaScript (ES6), 29 bytes

x=>t=>Math.sin(t+Math.PI/2*x)

Usage:

f=x=>t=>Math.sin(t+Math.PI/2*x)
f(x)(t)

Patrick Roberts

Posted 2016-08-13T18:44:12.947

Reputation: 2 475

1

Lua 5.x, 37 bytes

x,n=...return math.sin(x+math.pi/2*n)

usage:

> print(loadfile('sine.lua')(1,0))
0.8414709848079
> print(loadfile('sine.lua')(math.pi,0))
1.2246467991474e-16
> print(loadfile('sine.lua')(math.pi,1))
-1.0
> print(loadfile('sine.lua')(math.pi,2))
-2.4492935982947e-16
> print(loadfile('sine.lua')(math.pi,3))
1.0

thenumbernine

Posted 2016-08-13T18:44:12.947

Reputation: 341

1

Matlab, 19 bytes

@(x,t)sin(t+x*pi/2)

Alexander Kemp

Posted 2016-08-13T18:44:12.947

Reputation: 21

1So why the downvote? – Luis Mendo – 2016-08-14T23:59:48.853

0

SmileBASIC, 25 bytes

INPUT X,N?SIN(X+N*PI()/2)

Probably shorter in some BASIC dialect that has a constant named PI rather than PI(). (Yes, PI() is not actually a function, those parentheses are a lie.)

12Me21

Posted 2016-08-13T18:44:12.947

Reputation: 6 110

0

05AB1E, 7 bytes

žq*;+Ž

Port of @Mego♦'s Actually answer.

Try it online or verify some more test cases.

Explanation:

žq        # Push PI
  *       # Multiply it with the first (implicit) input `t`
   ;      # Halve it
    +     # Add the second (implicit) input `x`
     Ž   # Take the Sine of that (and output it implicitly)

Kevin Cruijssen

Posted 2016-08-13T18:44:12.947

Reputation: 67 575

0

SQF, 29 bytes

Using the file-as-a-function format:

params["x","y"];sin(x+pi/2*y)

Call as [x, t] call NAME_OF_COMPILED_FUNCTION

Οurous

Posted 2016-08-13T18:44:12.947

Reputation: 7 916