{ "cells": [ { "cell_type": "markdown", "id": "ad2fb634-5165-4ed5-8399-53ae0c994ffd", "metadata": { "tags": [] }, "source": [ "`help(r)` will show the help on R in the module `sage.interfaces.r` object." ] }, { "cell_type": "markdown", "id": "75ebc6ef-80e4-451f-a63c-3ab660405084", "metadata": {}, "source": [ "# 1. R is not a computer algebra system" ] }, { "cell_type": "code", "execution_count": 1, "id": "08fd56c6-5e4b-4e1d-bffe-b5cec983e361", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1] 10" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.choose(5, 2)" ] }, { "cell_type": "code", "execution_count": 2, "id": "1e3167bc-8087-4d93-973c-e45ef5661375", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1] 2.937234e+25" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.choose(100,30)" ] }, { "cell_type": "markdown", "id": "613e81ad-bdf0-49c3-af50-872032730488", "metadata": {}, "source": [ "We see that the output is not an exact number, but a floating-point approximation." ] }, { "cell_type": "markdown", "id": "c7e3d94f-b8f7-47b7-8a92-6a53a0ceab96", "metadata": {}, "source": [ "# 2. Monte Carlo integration" ] }, { "cell_type": "markdown", "id": "b7b700e9-3eb6-4977-9bee-5a438512e9a6", "metadata": {}, "source": [ "We generate one million random numbers uniformly distributed in the interval `[0,1]`." ] }, { "cell_type": "code", "execution_count": 3, "id": "228ef19a-2d1d-4d04-b2f8-58ec53556870", "metadata": { "tags": [] }, "outputs": [], "source": [ "u = r.runif(1000000, min=0, max=1)" ] }, { "cell_type": "code", "execution_count": 4, "id": "869e4561-7404-499f-be12-1716a2fc0f84", "metadata": { "tags": [] }, "outputs": [], "source": [ "v = r.sqrt(1-u^2)" ] }, { "cell_type": "markdown", "id": "89ee23a9-11fd-4eab-8925-5d91bce29969", "metadata": {}, "source": [ "Observe that `r` did vector arithmetic." ] }, { "cell_type": "code", "execution_count": 5, "id": "c4182ccc-8e2a-4c86-861b-dd29cd9d7bea", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1] 3.141743" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "estpi = 4*r.mean(v)\n", "estpi" ] }, { "cell_type": "code", "execution_count": 6, "id": "edaeb36e-5f0e-43bf-af33-429b23531de8", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(estpi)" ] }, { "cell_type": "code", "execution_count": 7, "id": "e2d6109d-a0cd-42f5-a348-6ec4941ed4fd", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3.14174254344342" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "estpi.sage()" ] }, { "cell_type": "markdown", "id": "32b7bdda-2b79-4dbe-9125-97a708cb743a", "metadata": {}, "source": [ "Let us consider another example." ] }, { "cell_type": "markdown", "id": "01a6e56e-8469-4a6a-bc2d-d48aed053d60", "metadata": {}, "source": [ "We estimate $\\displaystyle \\int_a^b f(x) dx$\n", "by $\\displaystyle (b-a) \\frac{1}{n} \\sum_{x \\in [a,b]} f(x)$.\n", "\n", "Applied to $\\displaystyle \\int_0^{\\pi/2} 4 \\sin(2 x) e^{-x^2} dx$:" ] }, { "cell_type": "code", "execution_count": 8, "id": "0a7a2345-862f-4d3b-96bb-ddd2ea49702c", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "1.57079632679490" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "halfpi = float(pi)/2.0\n", "halfpi" ] }, { "cell_type": "code", "execution_count": 9, "id": "a086e3f5-676c-4947-b131-ac15ff1096a5", "metadata": { "tags": [] }, "outputs": [], "source": [ "u = r.runif(1000000, min=0, max=halfpi)" ] }, { "cell_type": "code", "execution_count": 10, "id": "c79b24c3-ae28-4173-b471-9b2931d1397f", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1] 2.189133" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "halfpi*r.mean(4*sin(2*u)*exp(-u^2))" ] }, { "cell_type": "code", "execution_count": 11, "id": "b0013801-ba96-4ee0-a7d9-3ba0bbc701fb", "metadata": { "tags": [] }, "outputs": [], "source": [ "from rpy2.robjects import r as R" ] }, { "cell_type": "code", "execution_count": 12, "id": "ac7fb4e0-f369-4dfa-b062-1c75f85313d2", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ " [RTYPES.CLOSXP]\n", "R classes: ('function',)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R('f <-function(x) { 4*sin(2*x)*exp(-x^2) }')" ] }, { "cell_type": "code", "execution_count": 13, "id": "5a46537f-b58b-48ed-a344-da8a6ec947e9", "metadata": { "tags": [] }, "outputs": [], "source": [ "y = R('z = f(3)')" ] }, { "cell_type": "code", "execution_count": 14, "id": "1c4fdb55-c231-4b4b-96cd-7b8a8e1f21fa", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " FloatVector with 1 elements.\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " -0.000138\n", "
\n", " " ], "text/plain": [ " [RTYPES.REALSXP]\n", "R classes: ('numeric',)\n", "[-0.000138]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R('z')" ] }, { "cell_type": "code", "execution_count": 15, "id": "ee23f353-b25b-4b26-875c-6d2d8492c2c1", "metadata": { "tags": [] }, "outputs": [], "source": [ "v = R('val = integrate(f, lower=0, upper=pi/2)')" ] }, { "cell_type": "code", "execution_count": 16, "id": "1710f32c-15ce-4c0b-8e4f-16fc6cb803f2", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " ListVector with 5 elements.\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " value\n", " \n", " [RTYPES.REALSXP]\n", "
\n", " abs.error\n", " \n", " [RTYPES.REALSXP]\n", "
\n", " subdivisions\n", " \n", " [RTYPES.INTSXP]\n", "
\n", " message\n", " \n", " [RTYPES.STRSXP]\n", "
\n", " call\n", " \n", " [RTYPES.LANGSXP]\n", "
\n", " " ], "text/plain": [ " [RTYPES.VECSXP]\n", "R classes: ('integrate',)\n", "[FloatSe..., FloatSe..., IntSexp..., StrSexp..., LangSex...]\n", " value: \n", " [RTYPES.REALSXP]\n", " abs.error: \n", " [RTYPES.REALSXP]\n", " subdivisions: \n", " [RTYPES.INTSXP]\n", " message: \n", " [RTYPES.STRSXP]\n", " call: \n", " [RTYPES.LANGSXP]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R('val')" ] }, { "cell_type": "code", "execution_count": 17, "id": "90ec6a45-29e4-4b0c-8a31-9e618f6ade6a", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " FloatVector with 1 elements.\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " 2.190752\n", "
\n", " " ], "text/plain": [ " [RTYPES.REALSXP]\n", "R classes: ('numeric',)\n", "[2.190752]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[int(0)]" ] }, { "cell_type": "code", "execution_count": 18, "id": "df917703-67c6-4e0b-8c4f-5cea865b6e15", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2.190751642453249" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[int(0)][int(0)]" ] }, { "cell_type": "code", "execution_count": 19, "id": "f619ace2-184a-475d-b201-31e6c673e95f", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " FloatVector with 1 elements.\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " 0.000000\n", "
\n", " " ], "text/plain": [ " [RTYPES.REALSXP]\n", "R classes: ('numeric',)\n", "[0.000000]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[int(1)]" ] }, { "cell_type": "code", "execution_count": 20, "id": "5b167ddf-6f04-41bb-bbe7-557c3df0bd7d", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2.4322229146869754e-14" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[int(1)][int(0)]" ] }, { "cell_type": "code", "execution_count": 23, "id": "d2c0beb8-a55f-4e54-ab88-098c49edfdf3", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " ListVector with 2 elements.\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " x\n", " \n", " [RTYPES.REALSXP]\n", "
\n", " y\n", " \n", " [RTYPES.REALSXP]\n", "
\n", " " ], "text/plain": [ " [RTYPES.VECSXP]\n", "R classes: ('list',)\n", "[FloatSexpVector, FloatSexpVector]\n", " x: \n", " [RTYPES.REALSXP]\n", " y: \n", " [RTYPES.REALSXP]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R('plot(sin, -pi, pi)')" ] }, { "cell_type": "code", "execution_count": null, "id": "1a9e01fe-c2ab-471d-81cd-3549e326ba58", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.5", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }