Visualizza la regola della catena


10

Definizione

La regola della catena con due funzioni afferma che:

D[f(g(x))] = f'(g(x)) * g'(x)

Oppure, in alternativa:

D[f1(f2(x))] = f1'(f2(x)) * f2'(x)

La regola della catena con tre funzioni afferma che:

D[f(g(h(x)))] = f'(g(h(x))) * g'(h(x)) * h'(x)

Oppure, in alternativa:

D[f1(f2(f3(x)))] = f1'(f2(f3(x))) * f2'(f3(x)) * f3'(x)

Eccetera.

Compito

  • Dato un numero intero compreso tra 2 e 21, genera la regola della catena con tante funzioni, sia nella prima forma che nella seconda forma.
  • Si prega di specificare se si utilizza il secondo modulo.

Specifiche

  • Il formato della stringa deve essere esattamente quello sopra indicato, con:
    1. tutti gli spazi sono rimasti intatti
    2. un maiuscolo D
    3. una parentesi quadra immediatamente successiva D
    4. l'asterisco è rimasto intatto
  • È consentito uno spazio finale aggiuntivo (U + 0020).
  • Gli zeri iniziali nei nomi delle funzioni nella seconda forma (ad es. f01Anziché f1) sono consentiti.

Casi test

Se si utilizza il primo modulo:

input output
2     D[f(g(x))] = f'(g(x)) * g'(x)
3     D[f(g(h(x)))] = f'(g(h(x))) * g'(h(x)) * h'(x)

Se si utilizza il secondo modulo:

input output
2     D[f1(f2(x))] = f1'(f2(x)) * f2'(x)
3     D[f1(f2(f3(x)))] = f1'(f2(f3(x))) * f2'(f3(x)) * f3'(x)

Classifica


I nomi delle funzioni devono essere minuscoli?
Betseg,

@betseg Sì, certo.
Leaky Nun,

Risposte:


6

Python 2, 79 byte

f=lambda n:0**n*"D[x] ="or f(n-1).replace("x","f%d(x)"%n)+1%n*" *"+" f%d'(x)"%n

Uscite con funzioni numerate.

Crea l'output sostituendoli ripetutamente xcon fn(x), quindi aggiungendo * fn'(x). Il *è omessa per n==1.

Confronta con il programma iterato (92 byte):

r="D[x] = ";n=0
exec'n+=1;r=r.replace("x","f%d(x)"%n)+"f%d\'(x) * "%n;'*input()
print r[:-3]

96 byte:

n=input();r='';s='x'
while n:s='f%d(%%s)'%n%s;r=" * f%d'"%n+s[2:]+r;n-=1
print"D[%s] = "%s+r[3:]

Uscite con funzioni numerate.

Accumula la funzione nidificata f1(f2(f3(x)))in se l'espressione sul lato destro in r. La formattazione della stringa è voluminosa; Le stringhe f da 3.6 farebbero meglio.


5

Sesos , 49 byte

0000000: 2ac992 63fb92 245fb6 6c57be 255bbe 2cc9bf 6d49da  *..c..$_.lW.%[.,..mI.
0000015: 025e7f fdced0 fd67f8 fcde33 b6a7b2 643d4f 65597e  .^.....g...3...d=OeY~
000002a: f77a72 dd73cf fe                                  .zr.s..

Provalo online

smontato

set numin
add 68   ; 'D'
put
sub 7    ; '=' - 'D'
fwd 1
add 32   ; ' '
fwd 1
add 91   ; '['
put
add 2    ; ']' - '['
fwd 1
add 102  ; 'f'
fwd 1
add 40   ; '('
fwd 3
add 120  ; 'x'
rwd 2
get
jmp
    jmp
        fwd 1
        add 1
        rwd 3
        put
        add 1
        fwd 1
        put
        fwd 1
        sub 1
    jnz
    sub 1
    rwd 1
    add 1    ; ')' - '('
    fwd 3
    put
    rwd 1
    jmp
        rwd 3
        sub 1
        fwd 1
        put
        fwd 1
        add 1
        fwd 1
        sub 1
    jnz
    rwd 4
    put
    get
    add 41   ; ')'
    rwd 1
    put
    rwd 1
    put
    get
    add 42   ; '*'
    fwd 1
    put
    fwd 2
    put
    add 1
    fwd 1
    sub 2    ; '\'' - ')'
    put
    add 1    ; '(' - '\''
    put
    fwd 1
jnz
fwd 2
put
rwd 5
put

3

JavaScript (ES6), 89 byte

f=n=>--n?f(n).replace(/x/g,`${c=(n+15).toString(36)}(x)`)+` * ${c}'(x)`:`D[f(x)] = f'(x)`

O 75 byte usando il secondo modulo:

f=n=>n>1?f(n-1).replace(/x/g,`f${n}(x)`)+` * f${n}'(x)`:`D[f1(x)] = f1'(x)`

O 82/64 byte se mi è concesso un 1 *termine aggiuntivo :

f=n=>n?f(n-1).replace(/x/g,`${c=(n+14).toString(36)}(x)`)+` * ${c}'(x)`:`D[x] = 1`
f=n=>n?f(n-1).replace(/x/g,`f${n}(x)`)+` * f${n}'(x)`:`D[x] = 1`

1
Perché non metti la versione a 73 byte come principale?
Leaky Nun,

1
Se sei ricorsivo, hai bisogno f=altrimenti il ​​tuo programma non funzionerà.
Value Ink,

2
@ KevinLau-nonKenny Bah, mi dimentico sempre di farlo.
Neil,

1

Rubino, 72 byte

Seconda forma. Porta a Ruby dalla risposta @Neil.

f=->n{n>1?f[n-1].gsub(?x,"f#{n}(x)")+" * f#{n}'(x)":"D[f1(x)] = f1'(x)"}

1

Julia, 66 byte

!x=x>1?replace(!~-x,"x","f$x(x)")*" * f$x'(x)":"D[f1(x)] = f1'(x)"

La risposta ES6 di Port of @ Neil. Utilizza il secondo modulo.


0

Python 2, 174 byte

i=input()+1
a=lambda x:'('.join(['f%d'%e for e in range(x,i)])+'(x'+')'*(i-x)
print'D[%s] = %s'%(a(1),' * '.join(["f%d'%s%s)"%(e+1,'('*(i-e-2>0),a(e+2))for e in range(i-1)]))

Qui è rimasto molto spazio per giocare a golf. Utilizza la seconda forma ( f1, f2, ecc).


0

JavaScript (ES6), 194 byte

n=>{s="D[";for(i=1;i<=n;i++)s+="f"+i+"(";s+="x";s+=")".repeat(n);s+="] = ";for(i=1;i<=n;i++){s+="f"+i+"'(";for(j=i+1;j<=n;j++)s+="f"+j+"(";s+="x";s+=")".repeat(n-i+1);if(i-n)s+=" * ";}return s;}

31 byte salvati grazie a @LeakyNun.

È una funzione lambda anonima. Sono sicuro che c'è un modo per abbreviare questo ...

Ungolfed

var chain = function(n) {
    var str = "D["; // derivative symbol
    for (var i = 1; i <= n; i++) {
        str += "f"+i+"("; // put in n functions, each labeled f1(, f2(, etc.
    }
    str += "x"; // add in the function input, usually "x"
    for (var i = 0; i < n; i++) {
        str += ")"; // add in n end parentheses
    }
    str += "] = "; // add in the end bracket and the equals operator
    for (var i = 1; i <= n; i++) {
        str += "f"+i+"'("; // add in all n of the outer functions with the prime operator
        for (var j = i+1; j <= n; j++) {
            str += "f"+j+"("; // add in all of the inner functions
        }
        str += "x"; // add in the input, "x"
        for (var j = 1; j <= n; j++) {
            str += ")"; // close the parentheses
        }
        if (i !== n) {
            str += " * "; // the multiplication of all of the outer primed functions
        }
    }
    return str;
};

Cambia strin s.
Leaky Nun,

Ok, ho dimenticato di cambiarlo. Lol
Drew Christensen,

Cambia for(j=i;j<=n;j++)s+=")";ins+=")".repeat(n-i+1);
Leaky Nun,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.