# Write a Sine-Deriving Machine

### 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.

### 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.

• Are you sure d^-1 / dt^-1 (sin t) = -cos t ? – Leaky Nun Aug 13 '16 at 18:52
• What precision should this be to? – HyperNeutrino Aug 13 '16 at 18:58
• Test cases would be great. – Lynn Aug 13 '16 at 22:26
• 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 Aug 14 '16 at 0:46
• Today, I am more in the mood of writing an exponential function e^x deriving machine. – sergiol May 26 '17 at 21:37

## Actually, 5 bytes

╦*½+S


Try it online

Takes input as t\nx.

Explanation:

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


## 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.

• I'm such an idiot – Leaky Nun Aug 13 '16 at 19:12

# C (gcc), 30 bytes

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


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*)


# 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.

# Mathematica 14 bytes

With help from Martin Ender,

Sin[#2+Pi/2#]&

• Shouldn't x be #2? – Martin Ender Aug 13 '16 at 19:54
• At the same time, you don't need the *, so you'll end up with the same byte count. – Martin Ender Aug 13 '16 at 21:14

# 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.

• 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. Aug 13 '16 at 22:45
• @StevenH. Because stupid question requires negative numbers. – Leaky Nun Aug 14 '16 at 2:58

# Julia, 17 bytes

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


Try it online!

## R, 26 bytes

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


Exactly what it says on the tin...

# C (gcc), 5346 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.

• M_PI is not in math.h: stackoverflow.com/questions/5007925/…. – orlp Aug 13 '16 at 23:00
• But...it's there on glibc? sourceware.org/git/?p=glibc.git;a=blob;f=math/… – betseg Aug 13 '16 at 23:19
• Not 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 Aug 13 '16 at 23:24

# 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)


## Lua 5.x, 37 bytes

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


usage:

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


## Matlab, 19 bytes

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

• So why the downvote? – Luis Mendo Aug 14 '16 at 23:59

# 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

# 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.)

# 05AB1E, 7 bytes

žq*;+Å½


žq        # Push PI
*       # Multiply it with the first (implicit) input t
+     # Add the second (implicit) input x