Skip to main content

Posts

assignment - Setting parts of a list

Suppose I have list a = Range[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} in which I want to set some elements to be a list a[[4 ;; 7]] = {1, 2, 3}; {1, 2, 3, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, 8, 9, 10} Which is fine and dandy unless my part span is the same length as my list: a[[4 ;; 6]] = {1, 2, 3}; {1, 2, 3, 1, 2, 3, 7, 8, 9, 10} How can I force the assignment to behave consistently? In my case, I always want the first behaviour. But conceivably someone might always want the second behaviour with errors if the lengths don't match. Answer (a[[#]] = {1, 2, 3}) & /@ Range[4, 6]; You get: In[1]:= a Out[1]= {1, 2, 3, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, 7, 8, 9, 10} A convenient thing to remember is that if your elements are not sequential it is still easy to set up: (a[[#]] = {1, 2, 3}) & /@ {1, 3, 10}; In[2]:= a Out[2]= {{1, 2, 3}, 2, {1, 2, 3}, 4, 5, 6, 7, 8, 9, {1, 2, 3}}

evaluation - Apply a style to a selected structure

If I type out, in my notes, a series of transformations, I often like to convert the very last step to a Framed equation: I'm currently doing this by: selecting the last expression, copying it, pasting it in a new field, adding Framed[ … ] and evaluating, copying the result, pasting it back in the original field. Is there a hotkey or quick way for me to apply a transformation to a selection , replacing it with the result? (Or something like that.) Thanks! Answer Ctrl + Shift + Enter is the keyboard shortcut for "evaluate in place", which you press after highlighting an expression; this should ease your task. – J. M.♦ 2 mins ago Notice I used Defer , it holds its arguments and is stripped when boxes are created. Exactly what we need keep e.g. 1 + 1 in this form. Related: How can I change mouse hover behavior for hyperlinks in a notebook?

calculus and analysis - Residue failed to reproduce this residue in a paper

This problem is a more clearly state of my original problem which is off topic, so I reconsider my problem carefully based on previous discussions, and gives the following valid code to reproduce my problem with all the necessary information. I use Mathematica to confirm a result from a paper Unsteady unidirectional flow of Bingham fluid between parallel plates with different given volume flow rate conditions which is to calculate the residue of a expression at zero. Reproduce of my problem: The following code shows my efforts, but failed to reproduce the analytical solution ( resAna in the code) in the papaer. ClearAll["Global`*"] varCons = {ν > 0, up > 0, h > h0 >= 0, h > y >= 0}; m = Sqrt[s]/Sqrt[ν]; Δ = Sinh[m h] Sinh[m h0] - Cosh[m h] Cosh[m h0]; Ξ = Cosh[m h0] (Sinh[m h] - Sinh[m h0]) - Sinh[m h0] (Cosh[m h] - Cosh[m h0]); Ω = (m h (Δ + Cosh[m h0] Cosh[m y] - Sinh[m h0] Sinh[m y]))/(m h Δ + m h0 (Cosh[m h0]^2 - Sinh[m h0]^2) + Ξ); u = FullSimplify...

list manipulation - Interactive grid

I would like to create an interactive grid, whereby one number from a grid is selected by the cursor ("3" in the example below), and all other numbers in the grid are highlighted that are related to the chosen number, and each successive number after that. In the example below, "3" is the selected number, and the highlighted numbers are double the previous one. I am not sure whether a loop, or a direct function (in this case, clearly multiplying by powers of 2) would be the best approach - I assume the function-apply approach would be the quickest for large grids. Below is an example: generated with: m = 10;(*size of the grid*) premade = Transpose@Partition[Range@100, m]; col[n_] := Table[{n - 0.5, i - 0.5}, {i, 1, m}] Graphics[{ {Red, Opacity[0.3], Rectangle[{0, 2}, {01, 3}]}, {Red, Opacity[0.3], Rectangle[{0, 5}, {01, 6}]}, {Red, Opacity[0.3], Rectangle[{1, 1}, {02, 2}]}, {Red, Opacity[0.3], Rectangle[{2, 3}, {03, 4}]}, {Red, Opacity[0.3], Rectangle[{4,...

conditional - Evaluating an If condition to yield True/False

I would like to decide whether an option passed to my custom function has the value Automatic or something else. This is my attempt: f[x_, OptionsPattern[{DataRange -> Automatic}]]:= Module[{opt = OptionValue[DataRange]},{x, If[opt == Automatic, True, opt]}]; However, f[x, DataRange -> 20] produces {x, If[20 == Automatic, True, opt$540]} rather than the expected {x, 20} What do I need to change? Answer You need to use === (or SameQ ) instead of == (or Equal ) to test the condition. This is because === always returns True or False , whereas == can remain unevaluated. For example: a === b (* False *) a == b (* a == b *) The fact that == remains unevaluated is why it is useful in Solve , Reduce and related functions, where you can write an expression such as a x^2 + b x + c == 0 . Now, == does evaluate in cases such as comparisons between numeric quantities and strings or when the objects being compared are identical. For example: 1 == 1 (* True *) "abc" ==...

Chromatic number for "great circle" graph

I would like to calculate the chromatic number for a great circle graph. I tried this example in this link . I first tried to calculate the chromatic number for PetersenGraph as in the example but Mathematica did not give me the same result. I saw another example in this link . It worked for the example, but when I applied it to my great circle graph it did not work. Is there some special functions for great circle graphs?

probability or statistics - How to define a new copula distribution family

The function CopulaDistribution can be used with the most well known kernels. I was wondering how I can add a new family of kernels in a way that it works in the same way as the current function. For example as the documentation explains: CopulaDistribution can be used with such functions as Mean , PDF , and RandomVariate , etc. Do I need to define all these functions myself or is there a smart way of using the functions which are already defined in Mathematica? I wonder if anyone has done something similar and would like to share his/her experience on this.