Skip to content

Instantly share code, notes, and snippets.

@corba777
Created October 24, 2019 14:57
Show Gist options
  • Select an option

  • Save corba777/1df225393a13a000dca6206cfce7b958 to your computer and use it in GitHub Desktop.

Select an option

Save corba777/1df225393a13a000dca6206cfce7b958 to your computer and use it in GitHub Desktop.
DiffSharp Mergesort with loop.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"name": "ifsharp",
"display_name": "F#",
"language": "fsharp"
},
"language_info": {
"mimetype": "text/x-fsharp",
"nbconvert_exporter": "",
"name": "fsharp",
"pygments_lexer": "",
"version": "4.3.1.0",
"file_extension": ".fs",
"codemirror_mode": ""
},
"language": "fsharp",
"colab": {
"name": "DiffSharp Mergesort with loop.ipynb",
"provenance": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/corba777/1df225393a13a000dca6206cfce7b958/diffsharp-mergesort-with-loop.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "ajsm0HaVQ-M5",
"colab_type": "code",
"colab": {}
},
"source": [
"#load \"Paket.fsx\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "ST7D4CQrQ-M9",
"colab_type": "code",
"colab": {}
},
"source": [
"let wc = new System.Net.WebClient()\n",
"wc.DownloadFile(\n",
" \"https://github.com/DiffSharp/DiffSharp/releases/download/v0.7.7/DiffSharp-v0.7.7-Linux64.zip\", \n",
" \"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7-Linux64.zip\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "BuzOMp4GQ-NB",
"colab_type": "code",
"colab": {}
},
"source": [
"open System.Diagnostics"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "wI5qnbkBQ-NH",
"colab_type": "code",
"colab": {}
},
"source": [
"let ps = \n",
" ProcessStartInfo(\n",
" FileName=\"unzip\",\n",
" Arguments=\"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7-Linux64.zip -d /home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7\",\n",
" RedirectStandardOutput=true,UseShellExecute=false)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "U_Z1t8yaQ-NK",
"colab_type": "code",
"colab": {}
},
"source": [
"let p = Process.Start(ps)\n",
"p.StandardOutput.ReadToEnd()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "9piB3r5uQ-NO",
"colab_type": "code",
"colab": {}
},
"source": [
"#I \"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7/\"\n",
"#r \"DiffSharp\"\n",
"#r \"FSharp.Quotations.Evaluator\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "qk1YKBktQ-NS",
"colab_type": "code",
"colab": {}
},
"source": [
"let openblasFromNuget = @\"/home/nbuser/IfSharp/bin/packages/OpenBLAS/lib/native/bin/x64/\"\n",
"let diffSharpReleaseFromZip = @\"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7/\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "90Mri31uQ-NX",
"colab_type": "code",
"colab": {}
},
"source": [
"open System\n",
"Environment.SetEnvironmentVariable(\"LD_LIBRARY_PATH\", Environment.GetEnvironmentVariable(\"LD_LIBRARY_PATH\") + \":\" + diffSharpReleaseFromZip)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "Y2KiEeOuQ-Na",
"colab_type": "code",
"colab": {}
},
"source": [
"Environment.GetEnvironmentVariable(\"LD_LIBRARY_PATH\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "SJ5LxYkWQ-Nd",
"colab_type": "code",
"colab": {}
},
"source": [
"open DiffSharp.AD.Float64"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "uDC8jb7tQ-Ng",
"colab_type": "code",
"colab": {}
},
"source": [
"let split (arr : DV) =\n",
" let n = arr.Length\n",
" arr.[0..n/2-1], arr.[n/2..n-1] \n",
"\n",
"let rec merge (l : DV) (r : DV) =\n",
" let n = l.Length + r.Length\n",
" let res = Array.zeroCreate<D> n\n",
" let mutable i, j = 0, 0\n",
" for k = 0 to n-1 do\n",
" if i >= l.Length then res.[k] <- r.[j]; j <- j + 1\n",
" elif j >= r.Length then res.[k] <- l.[i]; i <- i + 1\n",
" elif l.[i] < r.[j] then res.[k] <- l.[i]; i <- i + 1\n",
" else res.[k] <- r.[j]; j <- j + 1\n",
" toDV res\n",
" \n",
"let rec mergeSort (arr :DV) =\n",
" if arr.Length<2 then arr else \n",
" let (x, y) = split arr\n",
" merge (mergeSort x) (mergeSort y) "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "hUdiMivIQ-Ni",
"colab_type": "code",
"colab": {}
},
"source": [
"let rnd=System.Random()\n",
"let test= Array.init 5 (fun _-> rnd.NextDouble()) |> toDV"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "ei7JldaTQ-Nk",
"colab_type": "code",
"colab": {},
"outputId": "544de7e4-9353-4c5c-f3f0-c1f17609a16f"
},
"source": [
"mergeSort test"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DV [|0.01764531434; 0.0457408936; 0.1075394187; 0.1250395524; 0.5259685705|]"
]
},
"metadata": {
"tags": []
},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "iSZ1tvPyQ-No",
"colab_type": "code",
"colab": {},
"outputId": "b8c80be9-bc30-4c5f-b130-da6eacb966af"
},
"source": [
"let jacobianmergesort=jacobian mergeSort\n",
"jacobianmergesort test"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DM [[0.0; 1.0; 0.0; 0.0; 0.0]\n",
" [0.0; 0.0; 0.0; 1.0; 0.0]\n",
" [1.0; 0.0; 0.0; 0.0; 0.0]\n",
" [0.0; 0.0; 0.0; 0.0; 1.0]\n",
" [0.0; 0.0; 1.0; 0.0; 0.0]]"
]
},
"metadata": {
"tags": []
},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "oL_RGAarQ-Nq",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment